2009-12-09 80 views
1

我的場景:
- 我正在多選模式下使用列表視圖,以使用戶能夠刪除他/她檢查過的幾個項目。
- 當用戶單擊刪除按鈕時,我執行以下操作:
- 使用以下位置獲取已檢查項目的位置:myList.getCheckedItemPositions();
- 獲取此位置的項目並將它們放入列表中 - toDeleteList。
- (基於此步驟的問題)使用myList.setItemChecked(位置, false)取消選中列表項目。
-Remove在「toDeleteList」
Android列表視圖問題澄清

現在的項目,我是「被迫」取消手動列表項,因爲myList.getCheckedItemPositions的結果()從MYLIST ..即

刪除後不會改變

例如,我刪除列表[a,b,c,d]的第一項(a),b 將在刪除後出現檢查即。在列表中列出[b,c,d] - 刪除a後。

問題是爲什麼?因爲SparseBooleanArray由 返回myList.getCheckedItemPositions(); 之前和一樣,使用適配器從列表中刪除。

我認爲(我可能是錯的),其通過適配器從列表中刪除 一個項目之後,CheckedItemPositions陣列也應該改變,以反映 列表

如新的狀態。 - MYLIST = [A,B,C,d]
- 然後我檢查項目位置0和3檢查(一個& d)
- 檢查的項目位置(mylist.getCheckedItemPositions())陣列現在 具有值如果我從列表中刪除了& d,因此,mylist = [b,c], mylist.getCheckedItemPositions()仍然與上面的ie相同,即,如果我刪除了& d。從列表中刪除的項目後,仍然檢查位置0和3(我認爲這是不正常的 - 但我可能是錯的)
- 我期待它不被檢查的位置0 & 3因爲 項目是以前在這些職位上已不在列表中。

我在這裏得到錯誤的東西(或有錯誤 期望:))?有人請事先弄清楚這個..
謝謝,

回答

1

講出我個人的看法,我假設仍然保持選中的位置,因爲getCheckedItemPositions()反映反對值的位置,因此,如果位置0和3仍然存在在ListView刪除後,他們將保持檢查。

我可能是錯的,只是表達了我自己的意見。祝你好運

+0

謝謝..我明白你的意思,但我想如果我在新列表中使用mylist.getCheckedItemPositions(),即。與以前檢查的項目不再在列表中(使用adapter.remove()..)後) - 結果應爲空/空,因爲我的列表不再包含選中的項目.. - 也許你是對的,謝謝 – cire 2009-12-09 17:00:51

+0

所有的東西你需要調用方法listView.getCheckItemIds(),它將返回checkeds項的id,它適用於我,而不會覆蓋任何東西;)。方法getcheckedItemPositions()不工作:) – Houcine 2012-01-14 16:25:44

2

我一直在努力解決這個問題,這對我來說很有效。

首先,我設置了一個遊標適配器,它根據它們的ID保持項目的狀態爲checked狀態,因爲當列表可以動態更改時,位置是沒有用的。

private class TagListAdapter extends SimpleCursorAdapter { 
    /** 
    * Stores boolean values for the list items, based on their ids. 
    */ 
    SparseBooleanArray mCheckStates; 

    public TagListAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     mCheckStates = new SparseBooleanArray(); 
    } 

    /** 
    * Sets an id as checked/unchecked. 
    * @param id 
    * @param checked 
    */ 
    public void setChecked(int id, boolean checked) { 
     mCheckStates.put(id, checked); 
    } 

    /** 
    * @see android.widget.ListAdapter#getView(int, View, ViewGroup) 
    */ 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 

     if(!mCursor.moveToPosition(position)) { 
      throw new IllegalStateException("couldn't move cursor to position " + position); 
     } 

     int id = mCursor.getInt(INDEX_OF_COLUMN_ID); 
     if(mCheckStates.indexOfKey(id) < 0) { 
      // Populate checked value for the first time. 
      mCheckStates.put(id, function_that_returns_if_the_item_is_checked(id)); 
     } 

     // Set the item as checked or unchecked based on the value stored in mCheckStates. 
     mListView.setItemChecked(position, mCheckStates.get(id, false)); 
     return view; 
    } 
} 

然後在我的活動我已經設置了執行相關選中/清除行動,並設置在適配器選中狀態的onListItemClick():

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    boolean checked = ((CheckedTextView) v).isChecked(); 

    if(checked == true) { 
     // execute uncheck related action 
     function_called_when_item_is_unchecked(id); 
    } else { 
     // execute check related action 
     function_called_when_item_is_checked(id); 
    } 

    // Check/uncheck the item in the adapter. 
    ((TagListAdapter) getListAdapter()).setChecked((int) id, !checked); 
} 
+0

我有同樣的問題,所以我認爲這可能適用於我,但我有兩個問題:1.你從哪裏得到mListView? 2.你能設置一個function_that_returns_if_the_item_is_checked的例子嗎? – 2010-11-02 18:28:08

+0

此外,這一行導致我的應用程序強制關閉(不知道爲什麼...):布爾checked =((CheckedTextView)v).isChecked(); – 2010-11-02 18:43:27