IsItemChecked always returns the opposite of what is assumed when inside the onItemClickListener?

I currently have a ListView that activates an ActionBar context in a ListView OnItemLongClickListener .

I am trying to make the elements selectable by clicking on them, but only when the Contextual ActionBar up.

The problem is that when I check isItemChecked() to toggle the item's selection state, it always returns the opposite of what it should.

This is how I implemented OnItemClickListener :

 list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (mActionMode != null){ list.setItemChecked(position, !list.isItemChecked(position)); } else{ list.setItemChecked(position, false); } } }); 

EDIT: This is pretty funny .. this code switches the selection state:

 list.setItemChecked(position, list.isItemChecked(position)); 

What's happening??

EDIT 2: And, it seems, the android automatically checks and removes each element on its own ... is there a way to change this behavior and process it yourself?

+8
android android-listview android-adapterview
source share
1 answer

here is the documentation for the setItemChecked method: Sets the checked state of the specified position.

in line

 list.setItemChecked(position, !list.isItemChecked(position)); 

you explicitly set it as opposed to what isItemChecked returns, negating the secoend argument in the statement

+1
source share

All Articles