我正在使用此方法检查列表中有多少项已检查,我收到此错误,即此方法不适用于任何早于 11 的 SDK。
API 8 级中 this 的等价物是什么
接受的答案对我不起作用(总是返回 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;
}
}
getCheckedItemIds().length
似乎可以解决问题
我正在使用我认为有效且适用于所有情况的代码:
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;
}
如果您发现它不起作用的情况,请通知我。