whats the equivilent of getCheckedItemCount() for API level < 11?

后端 未结 3 614
日久生厌
日久生厌 2021-01-05 05:15

I am using this method to check how many items on a list a checked and I get this error that this method is not available for any SDK older than 11.

What is the equi

3条回答
  •  北海茫月
    2021-01-05 05:53

    I'm using this code which I believe is efficient and works in all cases:

    public int getCheckedItemCount() {
        ListView listView = getListView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            return listView.getCheckedItemCount();
        }
    
        SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
        int count = 0;
        for (int i = 0, size = checkedItems.size(); i < size; ++i) {
            if (checkedItems.valueAt(i)) {
                count++;
            }
        }
        return count;
    }
    

    Please inform me if you find a case where it doesn't work.

提交回复
热议问题