【问题标题】:Wrong way to acess each checkbox in listView multichoice?访问listView多选中每个复选框的错误方法?
【发布时间】:2012-09-03 16:46:04
【问题描述】:

我有一个 listView 多选,每行列表都有一个复选框。我有一个 arrayList ,其中包含我想要在复选框上选中的列表位置。但是有些事情是错误的,因为不工作......

这是我的代码
listViewSocios->ListView
alPositionsOnList -> 数组列表

 CheckBox checkedBox;
    for(int i = 0 ; i<SessaoQuotaEdit.alPositionsOnList.size() ; i++){
        checkedBox = (CheckBox)((View)listViewSocios.getChildAt((Integer) SessaoQuotaEdit.alPositionsOnList.get(i))).findViewById(android.R.id.checkbox);
        checkedBox.setChecked(true);
    }

有什么想法吗?

在这一行给出一个错误

    checkedBox = (CheckBox)((View)listViewSocios.getChildAt((Integer) SessaoQuotaEdit.alPositionsOnList.get(i))).findViewById(android.R.id.checkbox);

【问题讨论】:

  • 所以你有一个多选 ListView 并且想要检查存储在 alPositionsOnList 中的行?你怎么定义alPositionsOnListList&lt;Integer&gt;
  • 你好,山姆。是 ArrayListalPositionsOnList = new ArrayList();我更新了 checkedBox = (CheckBox)((View)listViewSocios.getChildAt( SessaoQuotaEdit.alPositionsOnList.get(i))).findViewById(android.R.id.checkbox);删除演员,但不起作用....
  • 确切的例外是什么?可以发一下吗?

标签: android listview checkbox android-listview parent-child


【解决方案1】:

来自Android DocsListView有这个方法setItemChecked(int position, boolean value)

【讨论】:

  • Wingman 工作很好,谢谢,我不记得那样了。但是你知道我的旧代码有什么问题吗?我想它应该可以工作....
  • 在不知道你得到什么错误的情况下,很难说,但很可能你没有正确定位复选框。尝试在其中硬编码一个 1 作为索引,看看你是否可以真正检索复选框,或者你是否得到一个 null/casting 异常
  • @RicardoFilipe 您采用的方法并不安全。首先,您应该避免强制转换,只有当您 100% 确定列表项是您认为的那样时,并且只有当您从自己的自定义适配器提供这些项视图时才有可能。 ListView 还支持同一列表中的多种项目视图,以及页眉和页脚视图。
  • 感谢大家的时间和帮助 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
相关资源
最近更新 更多