How to get selected items from the Multi Select List

I am using an array adapter, and to this I am adding a list of arrays of string s, a list is a multiple choice. How can I get the values โ€‹โ€‹of list items?

my_contacts_list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,conts_list); my_contacts_list.setAdapter(adapter); 

I tried to do it

 SparseBooleanArray positions = my_contacts_list.getCheckedItemPositions(); int size=positions.size(); int i=0; while(i <= size){ conts_list.get(positions.get(i)); i++; } 

But position.get (i) is a list of arrays, how to get the selected elements?

+41
android android-listview
Jan 04 '11 at 5:15
source share
12 answers

SparseBooleanArray.get returns a boolean, but I believe that you need to check it for every position in your list, for example.

 int len = listView.getCount(); SparseBooleanArray checked = listView.getCheckedItemPositions(); for (int i = 0; i < len; i++) if (checked.get(i)) { String item = cont_list.get(i); /* do whatever you want with the checked item */ } 
+40
Jan 04 2018-11-11T00:
source share

This API is a mess. Here is what works for me.

 SparseBooleanArray checked = tags.getCheckedItemPositions(); for (int i = 0; i < checked.size(); i++) { if(checked.valueAt(i) == true) { Tag tag = (Tag) tags.getItemAtPosition(checked.keyAt(i)); Log.i("xxxx", i + " " + tag); } } 
+43
Dec 22 '11 at 2:15
source share

I believe that the fastest way to get information from this SparseArray is to SparseArray over the keys (in fact, I am sure that the above solutions will not work in all cases). ListView introduces a pair (index, true) in a SparseBooleanArray for each selected index.

So the code might look like this:

 SparseBooleanArray checked = lv.getCheckedItemPositions(); int size = checked.size(); // number of name-value pairs in the array for (int i = 0; i < size; i++) { int key = checked.keyAt(i); boolean value = checked.get(key); if (value) doSomethingWithSelectedIndex(key); } 
+17
Sep 13 '12 at 12:36
source share

I think Daren Robbins answer is wrong, here is my answer:

 ArrayList<String> ids = extras.getStringArrayList("commonids"); SparseBooleanArray checked = lv.getCheckedItemPositions(); for (int i = 0; i < checked.size(); i++) { if(checked.get(i)) Log.i("CheckedItem", ids.get(checked.indexOfKey(i))); } 

Suppose the identifiers are an arraylist with the same list size as the identifiers of the elements in the list view

+4
Dec 04 '11 at 11:00
source share

The fact is that you should iterate over all the elements of the list, but not check them.

Define the variables:

  • listView (instance of ListView)
  • names (ArrayList you)
  • saveCheckedName (save all verified name for this Arraylist)

      SparseBooleanArray checkedPositions = listView.getCheckedItemPositions(); for (int i = 0; i < subjectListView.getCount(); i++) { if (checkedPositions.get(i) == true) { saveCheckedName.add(names.get(i)); } } 
+3
Sep 26
source share

Like many others, multi-select ListView is a real problem in Android.

Instead of simply querying the selected items as a List of Object (dear Google, this is what we expect):

 List selected_items = my_list_view.getSelectedItems(); 

we are forced to use this incredibly funny API:

 SparseBooleanArray checked = my_list_view.getCheckedItemPositions(); int num_selected = 0; for(int i = 0; i < checked.size(); i++) { if(checked.valueAt(i)) { num_selected++; int key = checked.keyAt(i); boolean value = checked.get(key); if (value) { // } } } 

The SparseBooleanArray named SparseBooleanArray populated with a call to the even more horribly named getCheckedItemPositions() on the ListView . But instead of returning the position of each selected / checked item in the list, it returns the position of each item in the list that has been affected, regardless of whether it is currently selected or not! Incredible, but true.

In order to calculate whether an element is really checked ONLY IN THE CURRENT, we are forced to test valueAt(i) for plausibility, while the cycle of elements is โ€œever touchedโ€.

In addition to this madness, if we want to calculate the number of selected elements, it seems to us that we are forced to increase our own counter (for example, num_selected ).

With APIs like this, few people think about developers - this is anger!

+3
Jan 26 '16 at 17:15
source share

HOW I SOLVED THE NUMBER with a second ArrayList:

  • Second ArrayList instance created
  • Updated ArrayList instance with UNCHECKED elements.
  • added it to my list

     public void removeSelectedItems(){ updatedList = new ArrayList<String>(); //initialize the second ArrayList int count = lv.getCount(); //number of my ListView items SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions(); for (int i=0;i < count;i++){ if(!checkedItemPositions.get(i)) updatedList.add(liveNames.get(i)); //liveNames is the current ArrayList Log.e("TEST", liveNames.get(i)); } adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, updatedList); setListAdapter(adapter);} 

Hope this will be helpful :)

+1
Feb 10 '13 at
source share
 Foo objectAtCheckedRow = null; for (int i = 0; i < positions.size(); i++) { //positions.size() == 2 objectAtCheckedRow = adapter.getItem(positions.keyAt(i)); //Do something significant with object here } 

A few things to understand.

  • This is a list of key-value pairs.
  • Key is the row index, get it using position.keyAt (i)
  • Value : whether the string is checked in this index or not (true or false), get it using position.valueAt (i)
  • position.get (i) returns the same boolean as .valueAt (i)
  • Caution not to mix indices. You should not ( and should not ) iterate over the entire list. Use int i to iterate over the positions, but do not use i to get objects from your list.
  • But in this particular case (listView.getCheckedPositions ()) it only fills the true (marked lines), so you really don't need to check the use of .get (i) and .valueAt (i)

Example: Let's say you checked the 5th and 8th positions in the list (indices 4 and 7), then position.size () == 2 and I will be 0, and then 1

So when:

i == 0, then keyAt (i) == 4

i == 1, then keyAt (i) == 7

i == 0 OR i == 1, then valueAt (i) == true AND get (i) == true

+1
May 24 '13 at 6:00 a.m.
source share

We use this in our Android service class. Generics help prevent compiler warnings, but you can remove them if your adapter returns multiple types.

 public static <T> Collection<T> getCheckedItems(ListView listView) { Collection<T> ret = new ArrayList(); SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions(); for (int i = 0; i < checkedItemPositions.size(); i++) { if (checkedItemPositions.valueAt(i)) { T item = (T) listView.getAdapter().getItem(checkedItemPositions.keyAt(i)); ret.add(item); } } return ret; } 
+1
Aug 20 '16 at 15:17
source share

Very simple, use below code

 listViewRequests.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { AppCompatCheckedTextView checkBox = (AppCompatCheckedTextView) view; if (checkBox.isChecked() == true){ Log.i("CHECK",checkBox.isChecked()+""+checkBox.getText().toString()); } } }); 
+1
Aug 28 '17 at 6:19 on 06:19
source share

I think another option is to just keep an eye on all this.

  list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> listView, View selectedItem, int position, long itemId) { //Keep a reference here, and toggle a global variable. 
0
Apr 23 2018-12-12T00:
source share

Fyi. Here's how Google did it:

Excerpt from http://mytracks.googlecode.com/hg/MyTracks/src/com/google/android/apps/mytracks/util/Api11Adapter.java

 /** * Gets the checked positions in a list view. * * @param list the list view */ private int[] getCheckedPositions(ListView list) { SparseBooleanArray positions = list.getCheckedItemPositions(); ArrayList<Integer> arrayList = new ArrayList<Integer>(); for (int i = 0; i < positions.size(); i++) { int key = positions.keyAt(i); if (positions.valueAt(i)) { arrayList.add(key); } } int[] result = new int[arrayList.size()]; for (int i = 0; i < arrayList.size(); i++) { result[i] = arrayList.get(i); } return result; } 

and here is my adapted version:

 public static List<Integer> getAbsListViewCheckedItemPositions(AbsListView absListView) { SparseBooleanArray checked = absListView.getCheckedItemPositions(); List<Integer> positions = new ArrayList<>(); int checkedSize = checked.size(); for (int i = 0; i < checkedSize; i++) { if (checked.valueAt(i)) { positions.add(checked.keyAt(i)); } } return positions; } 
0
Jan 30 '15 at 8:11
source share



All Articles