2013-09-01 64 views
2

我遇到了由合併遊標填充的listview問題。 我在我的應用程序中有一個按鈕來選擇我的列表視圖中的所有條目。按下按鈕時調用的函數如下:listview setItemChecked無法正常工作

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

問題是,並非所有條目最後都會被檢查。確實很奇怪。經過一些測試後,我注意到如果我在mergecursor中有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(); 

總是返回視圖

+1

CursorAdapter使用數據庫中的_id列來標識行,而AbsListView使用該值來跟蹤哪些行被檢查。這聽起來像你的MergeCursor可能會返回重複的ID,例如,遊標A中第一行的_id值與遊標B中第一行的_id相同。是這種情況嗎? – alanv

+0

就是這樣,謝謝!看到我的評論如下 –

回答

3

爲alanv說, 條目數正確「的CursorAdapter使用_id列從您的數據庫,以確定行,並且AbsListView使用這個值來跟蹤哪些行被檢查,這聽起來像你的MergeCursor可能會返回重複的ID,例如,遊標A中第一行的_id值與遊標B中第一行的_id相同。那個案子?「

就是這樣。 改變光標後,以提供不同ID的所有工作好 但是:

  1. 合併光標是其他光標的串聯,不應該合併光標能夠用相同的ID從不同子來處理遊標(例如通過在內部創建新的ID)。
  2. 查看合併遊標的所有結果,並與它們進行交互(點擊它們)工作正常,所以我希望檢查它們以及它是否正常工作?