notifyDataSetChanged for multiple checkboxes

后端 未结 7 1609
一向
一向 2020-12-10 14:09

\"multiple_checkboxes\"

In attached image, SelectAll checkbox is present with in an activity, and

7条回答
  •  情歌与酒
    2020-12-10 14:56

    I tried it something like this,

    Passed the checkAll checkbox to the Adapter class constructor and setting is Listener there itself so that we don't need to declare any flag public static from the Main Class.

    Also I took couple of flags that maintain the state of the checkbox, that is I tried to maintain such that when the checkAll checkbox check is changed it does not effect the List Items checkbox and vice-versa for List Items checkbox check.

    So, try this

    public class myAdapter extends ArrayAdapter {
    
        private final List list;
        private final Activity context;
        private CheckBox checkAll;
        boolean checkAll_flag = false;
        boolean checkItem_flag = false;
    
        public myAdapter(Activity context, List list, CheckBox checkAll) {
            super(context, R.layout.row, list);
            this.context = context;
            this.list = list;
            this.checkAll = checkAll;
            checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton checkbox, boolean arg1) {
                    if(!checkItem_flag){
                        checkAll_flag = true;
                        notifyDataSetChanged(); 
                    }
                }
            });
        }
    
        static class ViewHolder {
            protected TextView text;
            protected CheckBox checkbox;
        }
    
        private boolean areAllSelected() {
    
             boolean areAllSelected = false;
    
              for (int i = 0; i < list.size(); i++) {
                  if(list.get(i).isSelected()){
                      areAllSelected = true;
                  }
                  else{
                      areAllSelected = false;
                      return areAllSelected;
                  }
              }
              return areAllSelected;
            }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view = null;
            if (convertView == null) {
                LayoutInflater inflator = context.getLayoutInflater();
                view = inflator.inflate(R.layout.row, null);
                final ViewHolder viewHolder = new ViewHolder();
                viewHolder.text = (TextView) view.findViewById(R.id.label);
                viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
                viewHolder.checkbox
                        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                            @Override
                            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                Model element = (Model) viewHolder.checkbox.getTag();
                                element.setSelected(buttonView.isChecked());
    
                                if(!checkAll_flag){
                                    checkItem_flag = true;
                                    if(buttonView.isChecked()){
                                        checkAll.setChecked(areAllSelected());
                                    }
                                    if(!buttonView.isChecked()){
                                        checkAll.setChecked(areAllSelected());                              
                                    }
                                    checkItem_flag = false;
                                }
                            }
                        });
                view.setTag(viewHolder);
                viewHolder.checkbox.setTag(list.get(position));
            } else {
                view = convertView;
                ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
            }
            ViewHolder holder = (ViewHolder) view.getTag();
            holder.text.setText(list.get(position).getName());
            holder.checkbox.setChecked(list.get(position).isSelected());    
    
            if(checkAll_flag){
                if(checkAll.isChecked()){
                    holder.checkbox.setChecked(true);
                }
                else if(!checkAll.isChecked()){
                    holder.checkbox.setChecked(false);
                }
                if(position == (list.size() -1)){
                    checkAll_flag = false;
                }
            }
            return view;
        }
    }
    

提交回复
热议问题