2

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

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();

总是在视图中返回正确数量的条目

4

1 回答 1

3

正如 alanv 所说,“CursorAdapter 使用数据库中的 _id 列来标识行,而 AbsListView 使用该值来跟踪检查了哪些行。听起来您的 MergeCursor 可能会返回重复的 ID,例如第一行的 _id 值游标 A 中的 _id 与游标 B 中第一行的 _id 相同。是这样吗?

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

  1. 合并游标是其他游标的串联,合并游标不应该能够处理来自不同子游标的相同ID(例如通过在内部创建新ID)。
  2. 查看合并光标的所有结果,并与它们交互(单击它们)工作正常,所以我希望检查它们是否正常工作?
于 2013-09-01T11:46:10.330 回答