Android ListView Multiple Choice Example

In android listview we can able to select more then one option at a time in list.
Here is a simple example for how to select more then one option from list.
Multiple Choice ListView Example :-
xml file
[sourcecode language="xml"]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:id="@+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
</LinearLayout>
[/sourcecode]
Java file
[sourcecode language="java"]
public class ListViewMultipleChoiceExample extends Activity {
private ListView lView;
private String lv_items[] = { "Android", "iPhone", "BlackBerry",
"AndroidPeople", "J2ME", "Listview", "ArrayAdapter", "ListItem",
"Us", "UK", "India" };

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lView = (ListView) findViewById(R.id.ListView01);
// Set option as Multiple Choice. So that user can able to select more the one option from list
lView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, lv_items));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
[/sourcecode]
The output will looks like

Comments

Hi Dzen, 1)You can use

Hi Dzen,
1)You can use ListViewName.setOnItemClickListener(listener) for action listener when user click on item. When user click on item automatically check box will checked. So you can use this method.
2)To get the List Checked Items use LVN.getCheckedItemPositions() or LVN.getCheckItemIds().
If you use LVN.getCheckedItemPositions() it will return as SparseBooleanArray from that array you can get the checked list.
If you use LVN.getCheckItemIds() it will return as long array, from that array you can get the checked list.

Questions ?

Thanks for your comments Dzen

Thanks for your comments Dzen :)

Thanks Reetha :) Cheers!!

Thanks Reetha :)
Cheers!!

Thanks for your code CM :) I

Thanks for your code CM :)
I hope your code will help to others for listview multiple choice selection.