【问题标题】:How to view the checked checkbox in listview?如何在列表视图中查看选中的复选框?
【发布时间】:2017-06-27 06:51:58
【问题描述】:

我在查看 android 中的选中复选框时遇到了问题

这是我的代码

  contactNumberList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkbox_number);

            AllContact contactNumber = contactsList.get(position);
            String num = contactNumber.getNumber();
            SelectedSpecificContact specificContact = new SelectedSpecificContact();
            specificContact.setNumber(num);

            if(!checkBox.isChecked())  {
                checkBox.setChecked(true);

                realmDbHelper.addSpecificSelectedNumber(specificContact);
                Log.e("number", num);
            }else{
                checkBox.setChecked(false);
                specificContact.setNumber(num);
                realmDbHelper.deleteAllSelectedData(specificContact);
            }
        }
    });

检查的列表项保存在数据库中。我想知道在列表视图中检查了哪些项目。 我已经在项目单击侦听器上初始化列表视图中的复选框。它仅在当前活动时显示选中的复选框。如果活动被破坏,则选中的复选框未选中。

当活动出现时如何查看选中的复选框。

【问题讨论】:

    标签: android listview android-checkbox


    【解决方案1】:

    设置checkbox.setChecked(true);条件是您的数据库返回真实条件。 假设数据库在星期一对选择返回 true,

      if (monday == 1) {
           chkMonday.setChecked(true);
           Object.setMonday(1);
      } 
    

    您可以在 onCreate() 或 onResume() 中执行此操作

    【讨论】:

      【解决方案2】:

      在您的 bean 类 is_active 中创建一个布尔或整数字段,就像在 SelectedSpecificContact.class 中一样。并将其值设置为1true,同时检查并存储在数据库中。

      下次从数据库中加载它们时,检查它是否为true。如果true 将可绘制状态设置为选中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        相关资源
        最近更新 更多