1

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.

r41n
  • 908
  • 7
  • 18
  • What do you mean by "it doesn't work"? There are several failure modes I can think of... You can still select multiple choices? The checked checkboxes are still displayed but you can only select one? The checked checkboxes are still displayed and you can select multiple ones? – Barak Jul 08 '12 at 11:30
  • Sorry Barak, I've edited the question to be more specific. – r41n Jul 09 '12 at 08:40

2 Answers2

5

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.

Community
  • 1
  • 1
Filip J.
  • 595
  • 6
  • 10
1

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); 
} 
Barak
  • 16,318
  • 9
  • 52
  • 84
  • Thank you, as of late I was focusing on more important parts of the app, so I didn't make any changes to that part of code. If i remember correctly I already tried to do exactly what you are suggesting, and it didn't work. Anyway, I'll give it a look and will let you know ASAP. – r41n Aug 12 '12 at 17:50
  • Sorry, took me quite some time to reply! As I thought, it didn't work. But I tried something new and by doing so noticed something that leads me to believe it's more a matter of refreshing the view or the adapter that's linked to the list. If I select items, then go back to CHOICE_MODE_NONE, and go to the "editing activity" (tap any item in the list), then go back to the "list activity", the selection of the list is not visible anymore. Just a hunch, but I'll give it a go ASAP. – r41n Aug 23 '12 at 17:06