简体   繁体   English

在列表视图中处理选中然后未选中的复选框的值

[英]Handle checked and then unchecked checkbox's value in listview android

In my activity I have 4 items in a list view with checkboxes in each row. 在我的活动中,列表视图中有4个项目,每行都有一个复选框。 After I leave activity I want all the checked item position numbers in an arraylist. 离开活动后,我希望所有选中的项目位置编号都在arraylist中。 This code works fine when user clicks all the checkboxes ones and leave the activity. 当用户单击所有复选框并退出活动时,此代码可以正常工作。 But when user clicks a checkbox and then unchecks it, item position is arraylist doesnt able to clear. 但是,当用户单击复选框然后取消选中该复选框时,arraylist的项目位置无法清除。 This methood is in my CustomAdapter class. 这种情况在我的CustomAdapter类中。

class GameAdapter extends BaseAdapter{
      private ArrayList<Integer> positions = new ArrayList<Integer>();

      public ArrayList<Integer> getPositions() {
          return positions;
      }

      getView(....){
         final ViewHolder holder;
         holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(holder.checkBox.isChecked()) {

                    positions.add(position);

                }
            }
        });
    }
}

I am handling this adapter class in the activity where my listview resides. 我在我的listview所在的活动中处理此适配器类。 There I am taking this arraylist values like this 我在这里采用这样的arraylist值

ArrayList<Integer> gamePositions = mGameAdapter.getPositions();
String itemNumbers = gamePositions.toString();

in itemNumber string I want all the checked item number values. 在itemNumber字符串中,我需要所有检查的项目编号值。

Thank you in advance for guidance.... 预先感谢您的指导。

Follow below approach. 请遵循以下方法。

  • Use HashMap instead of Arraylist, like below 使用HashMap代替Arraylist,如下所示

      HashMap<Integer,Boolean> mapOfSelection = new HashMap<>(); 
  • before onClick method, assign position to the checkbox, like below. onClick方法之前,将position分配给复选框,如下所示。

      holder.checkbox.setTag(position); 
  • In onClick, do the following. 在onClick中,执行以下操作。

      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { mapOfSelection.put((Integer)buttonView.getTag(),isChecked); // True if this position/value is checked, false otherwise. } 
  • Now when you need the selected items, just loop through the hashmap and check the positions/Key which have True value. 现在,当您需要选定的项时,只需遍历哈希图并检查具有True值的positions/Key

Replace this 取代这个

if(holder.checkBox.isChecked()) {

  positions.add(position);
}

with this 有了这个

 if(holder.checkBox.isChecked()) {
     if(!positions.contains(position))
        positions.add(position);

 } else {
     if(positions.contains(position))
         positions.remove(position);

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 处理已选中和未选中的复选框 - Handle checked and unchecked checkbox 为Checkbox for Android更改已选中和未选中的图标 - Change icons of checked and unchecked for Checkbox for Android 如何检查Android应用程序中的复选框是否已选中? - How to check if checkbox is checked or unchecked in android application? Android-如何在更改活动后保存复选框状态(已选中/未选中) - Android - How to save checkbox state (checked/unchecked) after changing activity 如何将复选框值(选中/未选中)作为jsp中的href参数传递给控制器 - how to pass checkbox value(checked/unchecked) to controller as href parameter in jsp 使用Android中的另一个CheckBox选中和取消选中所有CheckBox列的自定义列表视图? - Customized list view with Checked and unchecked all CheckBox columns by another CheckBox in android? Android Set不会删除arraylist中已选中然后未选中的值 - Android Set doesnt delete checked then unchecked value in arraylist 跟踪每次选中和未选中复选框的情况 - Keeping Track of Every Time that a Checkbox is checked and unchecked 从复选框获取未经检查和检查的值 - get unchecked and checked values from a checkbox SWT CheckBox按钮获得选中/取消选中状态 - SWT CheckBox Button get checked/unchecked state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM