Tutorial Programming

Tutorial ListView Checklist


Hallo sahabat saya akan menjelaskan tentang bagaimana menampilkan ListView dengan Checklist nah dengan contoh ada beberapa array yang akan saya tampilkan dan saat kita check list data tersebut akan manampilkan result tersebut. Oke langsung aja ke TKP

1. Buatlah project android kalian

2. Lalu copy coding MainActivity.java tersebut

package com.example.tutorchecklist;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class MainActivity extends Activity implements OnClickListener {
	final Context context = this;
	Button button;
	ListView listView;
	ArrayAdapter<String> adapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewsById();
		
		String negara[] = { "Indonesia","Malaysia","Brunei","Filipina",
      						"Thailand","Myanmar","Singapura","Vietnam",
      						"Kamboja","Laos" 
      		  			  };
		
		//String[] sports = getResources().getStringArray(R.array.sports_array);
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, negara);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listView.setAdapter(adapter);

		button.setOnClickListener(this);
	}

	private void findViewsById() {
		listView = (ListView) findViewById(R.id.list);
		button = (Button) findViewById(R.id.testbutton);
	}

	public void onClick(View v) {
		SparseBooleanArray checked = listView.getCheckedItemPositions();
		ArrayList<String> selectedItems = new ArrayList<String>();
		if(checked.size() !=0){
			for (int i = 0; i < checked.size(); i++) {
				// Item position in adapter
				int position = checked.keyAt(i);
				// Add sport if it is checked i.e.) == TRUE!
				if (checked.valueAt(i))
					selectedItems.add(adapter.getItem(position));
			}
			
			String[] outputStrArr = new String[selectedItems.size()];

			for (int i = 0; i < selectedItems.size(); i++) {
				outputStrArr[i] = selectedItems.get(i);
			}

			Intent intent = new Intent(getApplicationContext(),
					ResultActivity.class);

			// Create a bundle object
			Bundle b = new Bundle();
			b.putStringArray("selectedItems", outputStrArr);

			// Add the bundle to the intent.
			intent.putExtras(b);

			// start the ResultActivity
			startActivity(intent);
		}else if (checked.size() ==0){
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
	 
			// set title
			alertDialogBuilder.setTitle("Your Title");
	 
			// set dialog message
			alertDialogBuilder
				.setMessage("Click yes to exit!")
				.setCancelable(false)
				.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						// if this button is clicked, close
						// current activity
						MainActivity.this.finish();
					}
				})
				.setNegativeButton("No",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						// if this button is clicked, just close
						// the dialog box and do nothing
						dialog.cancel();
					}
				});
	 
			// create alert dialog
			AlertDialog alertDialog = alertDialogBuilder.create();
	 
			// show it
			alertDialog.show();	
		}
	}
}

3. Lalu copy coding ResultActivity.java tersebut

package com.example.tutorchecklist;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ResultActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);

		Bundle b = getIntent().getExtras();
		String[] resultArr = b.getStringArray("selectedItems");
		ListView lv = (ListView) findViewById(R.id.outputList);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, resultArr);
		lv.setAdapter(adapter);
	}
}

4. Lalu copy coding activity_main.xml tersebut

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/testbutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="@string/submit" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/testbutton"
        android:layout_alignParentTop="true" 
        />		
</RelativeLayout>

5. Lalu copy coding result.xml tersebut

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/outputList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />



6. Lalu copy AndroidManifest.xml tersebut


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tutorchecklist"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.tutorchecklist.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
            android:name=".ResultActivity"
            android:label="@string/result_activity" >
        </activity> 
    </application>

</manifest>



7. Lalu run aplikasi kalian.

8. Oke seleseai project kalian sahabat.







Silakan download project disini

Sekian tutorial saya semoga bermanfaat

No comments:

Post a Comment

Tutorial Programming Designed by Templateism | Ary EJFA Copyright © 2015

Theme images by richcano. Powered by Blogger.