【问题标题】:ViewBinder setViewValue for ListView item leads to multiple CheckBoxes checkedListView 项的 ViewBinder setViewValue 导致选中多个 CheckBoxes
【发布时间】:2011-09-23 20:40:14
【问题描述】:

我使用的是ListView,它有:

  1. 列表项点击
  2. CheckBox点击

我可以使用 view.setTag(cursor.getPosition()) 保存 cursorPosition 并且我可以对选中的项目采取必要的操作,但是当我向下滚动时,我看到其他几个 CheckBoxes 选中(仅视觉)。 作为一种解决方法,我尝试设置视图描述,将 CheckedBox 视图 ID 保存在列表中,然后迭代以查看 CheckBox 是否需要显示为选中状态。但是当我向下滚动时,视图似乎被重用(相同的视图 ID)。

我怎样才能只显示实际选中的CheckBoxes?代码:

public class MyViewBinder implements ViewBinder {
 public boolean setViewValue(View view, final Cursor cursor, int columnIndex) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.checkbox:
            view.setTag(cursor.getPosition());
            return true;

            case R.id.....
            .......
        }

用作:

mySimpleCursorAdapter.setViewBinder(myViewBinder);

【问题讨论】:

  • 您使用的是 ViewHolder 模式吗?一些代码会有所帮助:)
  • 已添加代码。此 Viewbinder 已按说明使用。

标签: android checkbox android-listview android-viewbinder


【解决方案1】:

我对 ViewBinder 接口没有太多经验,但您是否考虑过在列表视图 (API reference) 上使用 setChoiceMode()?您可以将其设置为CHOICE_MODE_MULTIPLE,以便 android 为您添加复选框。您不必担心以这种方式维护已检查的项目。

API Demo example.

【讨论】:

  • 这仅适用于我没有使用的 CheckedTextView。我正在使用我的自定义切换按钮。 CheckedTextView 的视图非常复杂
猜你喜欢
  • 2013-06-12
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多