开发者

ListView getCheckedItemPositions call issue

开发者 https://www.devze.com 2023-01-06 11:52 出处:网络
I am having issues with the getCheckedItemPositions call for ListView.The first call works but subsequent calls returned the same results even if the previous checked items were unchecked.

I am having issues with the getCheckedItemPositions call for ListView. The first call works but subsequent calls returned the same results even if the previous checked items were unchecked.

eg.

first call: positions 0 & 1 are checked, resu开发者_StackOverflowlt shows 0 & 1 are checked 2nd call: position 0 & 1 are unchecked, result still shows 0 & 1 are checked.

Is this a bug or getCheckedItemPositions doesn't work this way? Can someone clarify please? Thanks!


Use valueAt() instead of get(), then use keyAt() to find the right index to the checked itmes works for me.

SparseBooleanArray checkedItems = lview.getCheckedItemPositions();
if (checkedItems.size() > 0) {
 for (int i=0; i<checkedItems.size(); i++) {
  if (checkedItems.valueAt(i)) {
    Log.d("checked item: " + lview.getItemAtPosition(checkedItems.keyAt(i)));
  }
 }
} 


Recently I had the same problem and found the answer here:

How to use getCheckedItemPositions of ListView

To get the indices of the selected items of a multi-select ListView, you can use getCheckedItemPositions() to return a SparseBooleanArray.

The function however has a trap that is not documented, is that even you select one item and then deselect, the item is still included in the array, although the value of the item is set to false.

So as others have said, you need to iterate thru the returned SparseBooleanArray to find the TRUE values.

Sample code in the link.


I think that is meant to be called when you are closing the list, like when it's used in a dialog, so you can save the settings.

0

精彩评论

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