Skip to content Skip to sidebar Skip to footer

Is There A Simple Way To Check All Checkbox Items In Custimize Baseadapter In Android?

I defined a customize ImageAdapter with CheckBox control, I hope to click a button to check all CheckBox items in the ImageAdapter. Is there a simple way to do that? And more, I

Solution 1:

booleanflag=true;

Now on the button "select all" click, switch flag value:

flag = !flag;
adapter.notifydatasetchanged();

In your adapter Declare variables:

public ArrayList<String> checkedList;
    boolean flag = true;

    publicImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        checkedList = new ArrayList<>();
    }

In the getView method:

...
    final ViewHolder holder;
...
    holder.checkbox.setChecked(flag);
    holder.id = position;


    holder.checkbox.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            if (holder.checkbox.isChecked()) {
                holder.checkbox.setChecked(false);
            } else {
                holder.checkbox.setChecked(true);
            }
        }
    });

    holder.checkbox.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
        @OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                checkedList.add(Integer.toString(holder.id));
            } else {
                checkedList.remove(Integer.toString(holder.id));
            }
        }
    });
...

Adding those functions into your adapter

publicvoidsetSelectAll() {
        flag = true;
        notifyDataSetChanged();
    }

    publicvoidsetDeselectAll() {
        flag = false;
        notifyDataSetChanged();
    }

    publicArrayList<String> getCheckedList() {
        return checkedList;
    }

From your activity, just call adapter.setSelectAll() in onClick of your button, and adapter.getCheckedList() to handle your checked items.

Hope this work!

Post a Comment for "Is There A Simple Way To Check All Checkbox Items In Custimize Baseadapter In Android?"