Reputation: 938
I'm facing an odd behavior while trying to clear the choices (the selection) of items in my ListView. The code works pretty good, with functions that allow (once in CHOICE_MODE_MULTIPLE) to select single items by tapping, select all, select none and invert the current selection. Since the ListView is supposed to work in both modes (_NONE and _MULTIPLE) I have this menu item that switches between those modes allowing the user to "open" an item or select several items at once for batch operations.
The issue I'm facing shows up ONLY while changing choice mode from CHOICE_MODE_MULTIPLE back to CHOICE_MODE_NONE. What I'm trying to do is to not only revert back to CHOICE_MODE_NONE, but also clear any choices. The odd thing is that while all functions work reliably, when I call the "select non" function just before changing back to CHOICE_MODE_NONE, all items stay checked, no matter where or when I call the "select none" function inside my code.
The function that handles the selection changes is the following:
private void changeItemSelection(int selection) {
NotesAdapter adapter = (NotesAdapter)listView.getAdapter();
if (selection == SELECT_ALL) {
for(int iCount = 0; iCount < adapter.getCount(); iCount++) {
listView.setItemChecked(iCount, true);
}
}
else if (selection == SELECT_NONE) {
for(int iCount = 0; iCount < adapter.getCount(); iCount++) {
listView.setItemChecked(iCount, false);
}
}
else if (selection == SELECT_INVERT) {
for(int iCount = 0; iCount < adapter.getCount(); iCount++) {
listView.setItemChecked(iCount, !listView.isItemChecked(iCount));
}
}
adapter.notifyDataSetChanged();
checkedItemCountInvalid = true; // Invalidate checked indices cache
}
This is what happens when the user taps the menu item that switches selection modes:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_multisel:
toggleSelectionMode();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void toggleSelectionMode() {
if (listView.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
else {
listView.setChoiceMode(ListView.CHOICE_MODE_NONE);
}
changeItemSelection(SELECT_NONE);
}
No matter where I put "changeItemSelection(SELECT_NONE)" in the above code block, it simply doesn't work. But when I remove the "listView.setChoiceMode(ListView.CHOICE_MODE_NONE)" it magically starts to work as expected. It's driving me crazy... I really would appreciate any insights on this.
Thanks for reading!
EDIT: What I mean by "not working" is that the selection remains unchanged. Thus, if item1 and item3 where selected, calling select none doesn't uncheck them, but only when I call the function in the part of code mentioned above. Calling select none without trying to change choice mode works perfectly fine.
Upvotes: 1
Views: 2534
Reputation: 625
I experience the same problem. It seems that there already is related question having an answer that states that it is a bug in Android.
I suppose this should be a comment, not answer. But I just do not have enough reputation to comment here.
Upvotes: 5
Reputation: 16393
I don't know if you've solved this yet, so maybe this answer is too late.
I think if you try using clearChoices() before you set the choice mode back to none (and then call your method to clear the checks) it will work as you desire.
private void toggleSelectionMode() {
if (listView.getChoiceMode() == ListView.CHOICE_MODE_NONE) {
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
else {
listview.clearChoices();
listView.setChoiceMode(ListView.CHOICE_MODE_NONE);
}
changeItemSelection(SELECT_NONE);
}
Upvotes: 1