Reputation: 21
I am using a ListView with MULTIPLE_CHOICE
and to get the selected items back i am using setItemChecked()
method.
It works fine as i am able to see the previously checked items.
The issue is that if i uncheck one of the previously checked items, and then get the list of checked items by custList.getCheckItemIds()
the array still has the Item that i unchecked.
Can anyone please tell me if that is supposed to happen or am i missing something?
Upvotes: 2
Views: 2753
Reputation: 1
this:
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++){
if (checked.get(i))
//the item at index i is checked, do something
else
//the item is not checked, do something else
}
doens't work.
follow Multiple Contact Picker List [getCheckedItemPositions()]
should be OK.
SparseBooleanArray selectedPositions = listView.getCheckedItemPositions();
for (int i=0; i<selectedPositions.size(); i++) {
if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
//do stuff
}
}
Upvotes: 0
Reputation: 343
If you are simply trying to find out what items are checked at any given time, you can get a SparseBooleanArray from the ListView, and iterate over it with a for loop. For example:
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++){
if (checked.get(i))
//the item at index i is checked, do something
else
//the item is not checked, do something else
}
Upvotes: 0
Reputation: 917
Here you have to call setOnCheckedChangeListener and you have to manage the code inside this listener block.
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Write and manage your code here.
}
});
Upvotes: 1