How to select multiple item in ListView in android.
10 Answers
Actually you can ;) It's just a matter of user experience, right?
Try this, (1) for list control set
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemsCanFocus(false);
(2) define list item as
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:background="@drawable/txt_view_bg" />
This is same as android.R.layout.simple_list_item_multiple_choice
except
android:background="@drawable/txt_view_bg
(3) And define drawable txt_view_bg.xml as
<item android:drawable="@drawable/selected"
android:state_checked="true" />
<item android:drawable="@drawable/not_selected" />
Note:- The preferred way to handle multiple choice is to track choices your-self with on click item click, rather than depending on its state in list.

- 6,409
- 12
- 53
- 88

- 8,024
- 2
- 26
- 18
-
1I assume that the item elements above are in a selector element, and that "selected" and "not_selected" are user supplied drawables (like an image). Is there a way of using the platform's check boxes instead? – Diederik Feb 17 '11 at 14:33
-
2To use the platform's check boxes, remove the background attribute and ignore point (3). Although the boxes appear for me, unfortunately i am not able to actually check the box. I can do it only through the xml by using android:checked attribute to set a default value, but cannot change it at run time by touching it. – faizal Jun 02 '14 at 10:36
Step 1: setAdapter to your listview.
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, GENRES));
Step 2: set choice mode for listview .The second line of below code represents which checkbox should be checked.
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(2, true);
listView.setOnItemClickListener(this);
private static String[] GENRES = new String[] {
"Action", "Adventure", "Animation", "Children", "Comedy", "Documentary", "Drama",
"Foreign", "History", "Independent", "Romance", "Sci-Fi", "Television", "Thriller"
};
Step 3: Checked views are returned in SparseBooleanArray, so you might use the below code to get key or values.The below sample are simply displayed selected names in a single String.
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int arg2, long arg3)
{
SparseBooleanArray sp=getListView().getCheckedItemPositions();
String str="";
for(int i=0;i<sp.size();i++)
{
str+=GENRES[sp.keyAt(i)]+",";
}
Toast.makeText(this, ""+str, Toast.LENGTH_SHORT).show();
}

- 1,458
- 18
- 39

- 5,720
- 4
- 43
- 67
-
3if you unselect an item, it will continue to appear in the toast. So.. if you select 3 items, then unselect them and chose 1 other item, the string will contain all 4 items, even just 1 item is selected.I cannot explain why, because we always have a new String="". Does the `getCheckedItemPositions()` stores all the values that has been selected? but it would be strange. It would be nice to solve this , I have tried, but up to know I couldn't solve it. So, if somebody know, please share the solution. – AlexAndro Jul 30 '12 at 14:12
This example stores the values you have checked and displays them in a toast. And it updates when you uncheck items http://android-coding.blogspot.ro/2011/09/listview-with-multiple-choice.html

- 1,918
- 2
- 27
- 53
To "update" the Toast message after unchecking some items, just put this line inside the for loop:
if (sp.valueAt(i))
so it results:
for(int i=0;i<sp.size();i++)
{
if (sp.valueAt(i))
str+=names[sp.keyAt(i)]+",";
}

- 3,336
- 3
- 33
- 50
It's very simple,
listViewRequests.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
**AppCompatCheckedTextView checkBox = (AppCompatCheckedTextView) view;**
Log.i("CHECK",checkBox.isChecked()+""+checkBox.getText().toString());**
}
});

- 21
- 1
In listView you can use it by Adapter
ArrayAdapter<String> adapterChannels = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice);

- 318
- 3
- 10
Best way is to have a contextual action bar with listview on multiselect, You can make listview as multiselect using the following code
listview.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
And now set multichoice listener for Listview ,You can see the complete implementation of multiselect listview at Android multi select listview

- 68
- 4
- 16
and to get it :
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(getLocalClassName(), "onItemClick(" + view + ","
+ position + "," + id + ")");
}
});

- 2,258
- 4
- 20
- 20
I would advice to check the logic of ListActivity
according to what is needed could be the best way not to lose much time

- 6,405
- 5
- 39
- 42