listview setItemChecked无法正常工作

时间:2013-09-01 01:33:39

标签: android listview android-listview mergecursor

我遇到了由合并游标填充的列表视图的问题。 我的应用程序中有一个按钮,用于选择列表视图中的所有条目。按下按钮时调用的函数如下:

private void selectAllEntries() {
    int numberOfItemsInList = listView.getCount();
    for (int i = 0; i < numberOfItemsInList; i++) {
        this.listView.setItemChecked(i, true);
    }
}

问题是并非所有条目最终都会被检查。确实非常奇怪。经过一些测试后,我注意到如果我在mergecursor中有10个条目(光标a中有5个条目,光标b中有5个条目),那么如果我只在前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();

始终在视图中返回正确数量的条目

1 个答案:

答案 0 :(得分:3)

正如阿兰夫所说, “CursorAdapter使用数据库中的_id列来标识行,而AbsListView使用此值来跟踪检查哪些行。听起来你的MergeCursor可能正在返回重复的ID,例如光标A中第一行的_id值是与光标B中第一行的_id相同。是这种情况吗?“

情况确实如此。 更改光标以提供不同的ID后,一切正常 但是:

  1. 合并游标是其他游标的串联,合并游标不应该使用来自不同子游标的相同ID来处理(例如,通过在内部创建新的id)。
  2. 查看合并光标的所有结果,并与它们进行交互(单击它们)可以正常工作,所以我希望检查它们是否正常工作?