8

我正在使用此方法检查列表中有多少项已检查,我收到此错误,即此方法不适用于任何早于 11 的 SDK。

API 8 级中 this 的等价物是什么

4

3 回答 3

12

接受的答案对我不起作用(总是返回 0),我不得不使用以下代码:

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;
    }
}
于 2013-06-28T14:44:58.707 回答
5

getCheckedItemIds().length似乎可以解决问题

于 2013-01-07T10:21:39.740 回答
1

我正在使用我认为有效且适用于所有情况的代码:

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;
}

如果您发现它不起作用的情况,请通知我。

于 2014-11-10T09:09:22.713 回答