2009-12-09 81 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