【问题标题】:Can't capture Android Listview checkbox state无法捕获 Android Listview 复选框状态
【发布时间】:2014-02-25 15:48:49
【问题描述】:

尽我所能,我根本无法检测到我的 Listview 复选框的真/假(选中/未选中)状态。我正在即时构建这些,这可能是问题的一部分。

我的代码:

  1. 使用复选框构建列表视图:

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(                
     activity, android.R.layout.simple_list_item_multiple_choice, results);
    setListAdapter(adapter);
    for (int i = 0; i < activity.getListAdapter().getCount(); i++) {
                        activity.getListView().setItemChecked(i, true); //doesn't work BTW
    

    }

  2. 捕获复选框点击事件://从不显示已选中,即使它是

    @Override
    protected void onListItemClick(ListView lv, View view, int position, long id) {     
        super.onListItemClick(lv, view, position, id);
        CheckedTextView checkBox = (CheckedTextView)this.activity.getListView().getAdapter().getView(position, view, null);
    

非常感谢

最大

【问题讨论】:

  • 你试过设置这个吗,listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 正好低于你的setListAdapter(adapter);
  • 好吧,这对设置复选框很有用,所以谢谢你。我仍然无法在提交时选择正确的状态 - 显示为错误。我意识到我没有通过单击按钮发布代码。这是(下):
  • public void onClick(View v) { //搜索列表视图项目点击取消关注 LinearLayout ll = (LinearLayout)findViewById(R.id.listLayout); for (int i = 0; i

标签: android listview checkbox


【解决方案1】:

所以这就是我想出的:

在我的提交逻辑中,我无法获取复选框状态,因此我在 onItemClick 中对其进行跟踪

unfollowerState 是一个布尔数组,我用来跟踪检查状态...我在提交时选择它

protected void onListItemClick(ListView lv, View view, int position, long id) {     
    super.onListItemClick(lv, view, position, id);
    CheckedTextView checkBox = (CheckedTextView) view; //get the UI checkBox
    Log.i(LOG_TAG, ".onListItemClick" + checkBox.getText() + " " + checkBox.isChecked());
    unfollowerState[position-1]=checkBox.isChecked();  //record the check state
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-27
    • 2015-09-28
    • 1970-01-01
    • 2015-08-27
    • 2012-06-29
    • 1970-01-01
    • 2012-03-12
    • 2014-06-06
    相关资源
    最近更新 更多