title: Trouble with Android ListView selection changes when switching ChoiceMode
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>The function that handles the selection changes is the following:</p>
  
  <pre><code>private void changeItemSelection(int selection) {
      NotesAdapter adapter = (NotesAdapter)listView.getAdapter();
  
      if (selection == SELECT_ALL) {
          for(int iCount = 0; iCount &lt; adapter.getCount(); iCount++) {
              listView.setItemChecked(iCount, true);
          }
      }
      else if (selection == SELECT_NONE) {
          for(int iCount = 0; iCount &lt; adapter.getCount(); iCount++) {
              listView.setItemChecked(iCount, false);
          }
      }
      else if (selection == SELECT_INVERT) {
          for(int iCount = 0; iCount &lt; adapter.getCount(); iCount++) {
              listView.setItemChecked(iCount, !listView.isItemChecked(iCount));
          }       
      }
      adapter.notifyDataSetChanged();
      checkedItemCountInvalid = true; // Invalidate checked indices cache
  }
  </code></pre>
  
  <p>This is what happens when the user taps the menu item that switches selection modes:</p>
  
  <pre><code>@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);
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks for reading!</p>
  
  <p>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.</p>