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

扶醉桌前 提交于 2019-11-30 03:41:16

问题


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 equivalent this in API level 8


回答1:


getCheckedItemIds().length seems to do the trick




回答2:


The accepted answer didn't work for me (always returns 0), I had to use the following code:

public static int getCheckedItemCount(ListView listView)
{
    if (Build.VERSION.SDK_INT >= 11) return listView.getCheckedItemCount();
    else
    {
        int count = 0;
        for (int i = listView.getCount() - 1; i >= 0; i--)
            if (listView.isItemChecked(i)) count++;
        return count;
    }
}



回答3:


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.



来源:https://stackoverflow.com/questions/12330660/whats-the-equivilent-of-getcheckeditemcount-for-api-level-11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!