Reputation: 11
I have a ListView
which am setting that to my adapter. The ListView
item contains two view elements which are chekbox
and TextView
.
I have given the setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)
for the ListView
.
When i want to select one or more element, I want to get the count (based on selecting and deselecting).
I can't do this stuff in onItemClickListener
of ListView
.
So I have written the logic in BaseAdapter.
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count += 1;
}
});
But If i deselect the item then the value have to decrease. If iam having only one item in ListView
then I can write
SparseBooleanArray checked = listView.getCheckedItemPositions();
and get the value in Fragment. Just I get confused. Could someone help me?
Upvotes: 1
Views: 1642
Reputation: 2281
Use this.
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int count = 0;
for (int i = 0, ei = checkedItemPositions.size(); i < ei; i++) {
if (checkedItemPositions.valueAt(i)) {
count++;
}
}
// use count as you wish
Make sure count
is in a local (or block of the OnClickListener) scope so that every time you click a button or something, count gets reset and it recounts how many items are checked/unchecked.
Let me know if you have more question :)
Upvotes: 2