Skip to content Skip to sidebar Skip to footer

Notifydatasetchanged For Multiple Checkboxes

In attached image, SelectAll checkbox is present with in an activity, and all checkboxes within adapter. If i checked SelectAll then all checkboxes are checked in adapter and if I

Solution 1:

I did like below and I got my required output,

http://pastebin.com/PgNeDnXq

Solution 2:

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

publicclassmyAdapterextendsArrayAdapter<Model> {

    privatefinal List<Model> list;
    privatefinal Activity context;
    private CheckBox checkAll;
    booleancheckAll_flag=false;
    booleancheckItem_flag=false;

    publicmyAdapter(Activity context, List<Model> list, CheckBox checkAll) {
        super(context, R.layout.row, list);
        this.context = context;
        this.list = list;
        this.checkAll = checkAll;
        checkAll.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {

            @OverridepublicvoidonCheckedChanged(CompoundButton checkbox, boolean arg1) {
                if(!checkItem_flag){
                    checkAll_flag = true;
                    notifyDataSetChanged(); 
                }
            }
        });
    }

    staticclassViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    privatebooleanareAllSelected() {

         booleanareAllSelected=false;

          for (inti=0; i < list.size(); i++) {
              if(list.get(i).isSelected()){
                  areAllSelected = true;
              }
              else{
                  areAllSelected = false;
                  return areAllSelected;
              }
          }
          return areAllSelected;
        }

    @Overridepublic View getView(finalint position, View convertView, ViewGroup parent) {
        Viewview=null;
        if (convertView == null) {
            LayoutInflaterinflator= context.getLayoutInflater();
            view = inflator.inflate(R.layout.row, null);
            finalViewHolderviewHolder=newViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.checkbox
                    .setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {

                        @OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            Modelelement= (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));
        }
        ViewHolderholder= (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);
            }
            elseif(!checkAll.isChecked()){
                holder.checkbox.setChecked(false);
            }
            if(position == (list.size() -1)){
                checkAll_flag = false;
            }
        }
        return view;
    }
}

Solution 3:

Solution 4:

You can make an array of booleans corresponding to list of items and in the adapter you should toggle the boolean values accordingly checked/unchecked, and in adapter on each and every check that is all elements of boolean array is true? then setchecked(true) the selectall_checkbox else keep it setchecked(unchecked).

Solution 5:

EDIT

Your adapter should look like this:

publicclassInteractiveArrayAdapterextendsArrayAdapter<Model> {
  privatefinal List<Model> list;
  privatefinal Activity context;
  private CheckBox selectAll;

  publicInteractiveArrayAdapter( Activity context, List<Model> list, CheckBox selectAll ) {
    super( context, R.layout.list_items_attendance_payment, list );
    this.context = context;
    this.list = list;
    this.selectAll = selectAll;
  }

  staticclassViewHolder {
    protected TextView text;
    protected CheckBox checkbox;
  }

  @Overridepublic View getView( finalint position, View convertView, ViewGroup parent ) {
    Viewview=null;

    if( convertView == null ) {
      LayoutInflaterinflator= context.getLayoutInflater();
      view = inflator.inflate( R.layout.list_items_attendance_payment, null );
      finalViewHolderviewHolder=newViewHolder();
      viewHolder.text = (TextView) view.findViewById( R.id.name );
      viewHolder.checkbox = (CheckBox) view.findViewById( R.id.check );
      view.setTag( viewHolder );
    }
    else {
      view = convertView;
    }

    ViewHolderholder= (ViewHolder) view.getTag();
    holder.text.setText( list.get( position ).getName() );

    viewHolder.checkbox.setOnCheckedChangeListener( newCompoundButton.OnCheckedChangeListener() {
      @OverridepublicvoidonCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
        AttendanceActivity.listMember.get( position ).setSelected( isChecked );
        list.get( position ).setSelected( buttonView.isChecked() );
        selectAll.setChecked( areAllSelected() ); // We just use the method as parameter as it returns a boolean.
      }
    } );

    if( selectAll.isChecked() )
      holder.checkbox.setChecked( true );
    else
      holder.checkbox.setChecked( list.get( position ).isSelected() );

    return view;
  }

  publicvoiddeselectAll() {
    for( Model element : list )
      element.setSelected( false );

    notifyDataSetChanged();
  }

  publicvoidselectAll() {
    for( Model element : list )
      element.setSelected( true );

    notifyDataSetChanged();
  }

  privatebooleanareAllSelected() {
    for( Model element : list )
      if( !element.getSelected() )
        returnfalse; // We conclude that not all are selected.returntrue; // All of the items were selected
  }
}

And the method in your Activity that set the OnCheckedChangedListener should be changed to this:

privatevoidonClickSetAllAttendance() {
  selectAll.setOnClickListener( newOnClickListener() {
    @OverridepublicvoidonClick( View v ) {
      CheckBox box = (CheckBox) v;
      box.toggle();

      if( box.isChecked() )
        listAdapter.selectAll();
      else
        listAdapter.deselectAll();
    }
  });
}

Post a Comment for "Notifydatasetchanged For Multiple Checkboxes"