我遇到了由合并光标填充的列表视图的问题。我的应用程序中有一个按钮可以选择列表视图中的所有条目。按下按钮时调用的函数如下:
private void selectAllEntries() {
int numberOfItemsInList = listView.getCount();
for (int i = 0; i < numberOfItemsInList; i++) {
this.listView.setItemChecked(i, true);
}
}
问题是最终并非所有条目都得到检查。确实很奇怪。经过一些测试,我注意到如果我在合并光标中有 10 个条目(5 个来自光标 a,5 个来自光标 b),那么如果我只在前 5 个元素上使用 setItemChecked,一切正常(这 5 个条目被检查)。如果我再次在最后 5 个元素上使用 setItemchecked 一切正常(来自游标 b 的条目被检查),但如果我对来自两个游标的元素使用 setItemChecked 则被检查的最大元素数是游标 a 中的元素数(在我们的示例中为 5),偏移量为(我想要设置的项目数 - 光标 a 中的项目数)。我会写下几个例子,因为这是一个非常奇怪的行为。
Example 1:
element 0 (from cursor a, unchecked)
element 1 (from cursor a, unchecked)
element 2 (from cursor a, unchecked)
element 3 (from cursor a, unchecked)
element 4 (from cursor a, unchecked)
element 5 (from cursor b, unchecked)
element 6 (from cursor b, unchecked)
element 7 (from cursor b, unchecked)
element 8 (from cursor b, unchecked)
element 9 (from cursor b, unchecked)
setItemChecked(0,true);
setItemChecked(1,true);
setItemChecked(2,true);
setItemChecked(3,true);
setItemChecked(4,true);
Results:
element 0 (from cursor a, checked)
element 1 (from cursor a, checked)
element 2 (from cursor a, checked)
element 3 (from cursor a, checked)
element 4 (from cursor a, checked)
element 5 (from cursor b, unchecked)
element 6 (from cursor b, unchecked)
element 7 (from cursor b, unchecked)
element 8 (from cursor b, unchecked)
element 9 (from cursor b, unchecked)
Example 2:
element 0 (from cursor a, unchecked)
element 1 (from cursor a, unchecked)
element 2 (from cursor a, unchecked)
element 3 (from cursor a, unchecked)
element 4 (from cursor a, unchecked)
element 5 (from cursor b, unchecked)
element 6 (from cursor b, unchecked)
element 7 (from cursor b, unchecked)
element 8 (from cursor b, unchecked)
element 9 (from cursor b, unchecked)
setItemChecked(5,true);
setItemChecked(6,true);
setItemChecked(7,true);
setItemChecked(8,true);
setItemChecked(9,true);
results:
element 0 (from cursor a, unchecked)
element 1 (from cursor a, unchecked)
element 2 (from cursor a, unchecked)
element 3 (from cursor a, unchecked)
element 4 (from cursor a, unchecked)
element 5 (from cursor b, checked)
element 6 (from cursor b, checked)
element 7 (from cursor b, checked)
element 8 (from cursor b, checked)
element 9 (from cursor b, checked)
Example 3:
element 0 (from cursor a, unchecked)
element 1 (from cursor a, unchecked)
element 2 (from cursor a, unchecked)
element 3 (from cursor a, unchecked)
element 4 (from cursor a, unchecked)
element 5 (from cursor b, unchecked)
element 6 (from cursor b, unchecked)
element 7 (from cursor b, unchecked)
element 8 (from cursor b, unchecked)
element 9 (from cursor b, unchecked)
setItemChecked(0,true);
setItemChecked(1,true);
setItemChecked(2,true);
setItemChecked(3,true);
setItemChecked(4,true);
setItemChecked(5,true);
results in
element 0 (from cursor a, unchecked)
element 1 (from cursor a, checked)
element 2 (from cursor a, checked)
element 3 (from cursor a, checked)
element 4 (from cursor a, checked)
element 5 (from cursor b, checked)
element 6 (from cursor b, unchecked)
element 7 (from cursor b, unchecked)
element 8 (from cursor b, unchecked)
element 9 (from cursor b, unchecked)
Example 4:
element 0 (from cursor a, unchecked)
element 1 (from cursor a, unchecked)
element 2 (from cursor a, unchecked)
element 3 (from cursor a, unchecked)
element 4 (from cursor a, unchecked)
element 5 (from cursor b, unchecked)
element 6 (from cursor b, unchecked)
element 7 (from cursor b, unchecked)
element 8 (from cursor b, unchecked)
element 9 (from cursor b, unchecked)
setItemChecked(0,true);
setItemChecked(1,true);
setItemChecked(2,true);
setItemChecked(3,true);
setItemChecked(4,true);
setItemChecked(5,true);
setItemChecked(6,true);
results:
element 0 (from cursor a, unchecked)
element 1 (from cursor a, unchecked)
element 2 (from cursor a, checked)
element 3 (from cursor a, checked)
element 4 (from cursor a, checked)
element 5 (from cursor b, checked)
element 6 (from cursor b, checked)
element 7 (from cursor b, unchecked)
element 8 (from cursor b, unchecked)
element 9 (from cursor b, unchecked)
Example 5:
element 0 (from cursor a, unchecked)
element 1 (from cursor a, unchecked)
element 2 (from cursor a, unchecked)
element 3 (from cursor a, unchecked)
element 4 (from cursor a, unchecked)
element 5 (from cursor b, unchecked)
element 6 (from cursor b, unchecked)
element 7 (from cursor b, unchecked)
element 8 (from cursor b, unchecked)
element 9 (from cursor b, unchecked)
setItemChecked(0,true);
setItemChecked(1,true);
setItemChecked(2,true);
setItemChecked(3,true);
setItemChecked(4,true);
setItemChecked(5,true);
setItemChecked(6,true);
setItemChecked(7,true);
results:
element 0 (from cursor a, unchecked)
element 1 (from cursor a, unchecked)
element 2 (from cursor a, unchecked)
element 3 (from cursor a, checked)
element 4 (from cursor a, checked)
element 5 (from cursor b, checked)
element 6 (from cursor b, checked)
element 7 (from cursor b, checked)
element 8 (from cursor b, unchecked)
element 9 (from cursor b, unchecked)
我正在使用
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
和
notifyDataSetChanged()
也无济于事。有什么帮助吗?
作为旁注,
listView.getCount();
总是在视图中返回正确数量的条目