开发者

Listview.getCheckedItemPositions() not returning correct result when item is unchecked

I used SparseBooleanArray to get positions of checked listitems in a listView:

// lv is my listview
final SparseBooleanArray checkedItems = lv.getCheckedItemPositions();

This works fine when I check a list item. But when I unchecked an item from listview, its size didn't decrease. It remained as it was.

Let me explain what is happening:

I firstly select 3 listitems and I get the size of checkedItems as 3. But when I uncheck one item from the list, its size doesn't 开发者_高级运维change to 2. It is still 3.

What needs to be done? Kindly help me out.

Stone


SparseBooleanArray maps integers to booleans. The size() method does not return the number of true items contained, it returns the number of items stored. Some of those items may be false.

If you want to know the number of items checked, you can iterate over the SparseBooleanArray, you can track the number of checked items by calling isItemChecked() when checked state changes, or if you are on API 11+ (Honeycomb) you can call getCheckedItemCount().


The SparseBooleanArray does not contain the details of all the rows. The keys contain the index of the row and the value contains if that is checked. If one row was checked and then unchecked, the array will contain one entry for that row. So to deal with this you need to do the following:

  1. Get the size by calling int lSize = ObjectOfSparseBooleanArray.size().
  2. Run a loop as for(int i = 0; i < lSize; i++) {
  3. Get the row number int lPos = ObjectOfSparseBooleanArray.keyAt(i);
  4. Get is the row is checked boolean lIsChecked = ObjectOfSparseBooleanArray.get(lPos);
  5. Do your processing for the selected rows!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜