Return Values ​​from Multiple Selection ListView

Edit: Ok, I found a solution. I do not know that this is the right solution, but it works correctly. Added code below.

I am trying to allow the user to select multiple directories from the checklist and return them when the "Submit" button is clicked. Here is a snippet of my code. It populates the ListView with all the directories on / sdcard /, and for the initial selection (no matter what I choose), when I submit, the log shows the correct choice. However, if I uncheck an item and click Submit again, it will still show as if everything was selected. Do I need to write a handler to uncheck? I thought I took care of choosing choiceMode? Thank!

private SparseBooleanArray a; directoryList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, directoryArray)); submitButton = (Button)findViewById(R.id.submit_button); submitButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { a = new SparseBooleanArray(); a.clear(); a = directoryList.getCheckedItemPositions(); for (int i = 0; i < a.size(); i++) { //added if statement to check for true. The SparseBooleanArray //seems to maintain the keys for the checked items, but it sets //the value to false. Adding a boolean check returns the correct result. if(a.valueAt(i) == true) Log.v("Returned ", directoryArray[a.keyAt(i)]); } } }); 
+10
android listview multiple-select
Aug 12 2018-10-12T00:
source share
4 answers

I did a few more debugs and found a solution that worked for me. Edited in the code above. For some reason, SparseBooleanArray is not freeing itself; It supports keys of checked boxes. However, when getCheckedItemPositions () is called, it sets VALUE to false. So the key is still in the returned array, but it is false. Only marked fields will be marked true.

+3
Aug 13 '10 at 3:16
source share

I know that you have found a solution that works for you, but a cleaner and simpler solution that will probably work most of the time is this (I want to keep all the identifiers of the selected elements):

(in my ListActivity):

 SparseBooleanArray selectedPos = getListView() .getCheckedItemPositions(); ListAdapter lAdapter = getListAdapter(); List<Long> ids = new LinkedList<Long>(); for (int i = 0; i < lAdapter.getCount(); i++) { if (selectedPos.get(i)) { ids.add(lAdapter.getItemId(i)); } } 
+4
Oct 11 2018-11-11T00:
source share

It didn’t mean doing this as an answer, but I had to expand on what you did to make multi select. Why did you make a field variable for your choices? I just made a local SparseBooleanArray ...

 public class NaughtyAndNice extends ListActivity { TextView selection; String[] items={"lorem","ipsum", "dolor", "sit", "amet", "consectetuer", "adipisc", "jklfe", "morbi", "vel", "ligula", "vitae", "carcu", "aliequet"}; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,items)); selection = (TextView)findViewById(R.id.selection); this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); } public void onListItemClick(ListView parent, View view, int position, long id){ SparseBooleanArray choices = parent.getCheckedItemPositions(); StringBuilder choicesString = new StringBuilder(); for (int i = 0; i < choices.size(); i++) { //added if statement to check for true. The SparseBooleanArray //seems to maintain the keys for the checked items, but it sets //the value to false. Adding a boolean check returns the correct result. if(choices.valueAt(i) == true) choicesString.append(items[choices.keyAt(i)]).append(" "); } selection.setText(choicesString); } } 

+1
Dec 07 2018-10-12T00:
source share

No need to use SparseBooleanArray choices = parent.getCheckedItemPositions();

StringBuilder enough for this.

+1
Dec 08 '11 at 7:50
source share



All Articles