How to implement a button that gets all checkbox’s state and adds the value of checked item into arraylist?

I have already messed up with the similar problem. You can initially call a function in your constructor which initialize the states of your checkboxes as false and save all states in your arraylist. Whenever checkbox will be clicked just check the box and save its state in boolean arraylist.
Later you can retrieve your states from arraylist.
If you havn’t understand then let me know I will try to send some lines of code to help you…

Here is a little code for your help:
I will create a boolean arraylist.

private ArrayList<Boolean> itemChecked      = null;

Then I will set states of all checkboxes as false in my constructor:

    for (int i=0; i < no_of_elements.size(); i++) {
        itemChecked.add(i, false);
    }

Set the actual checked state when checkbox clicked:

        holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                itemChecked.set(position, isChecked);

            }
        });

Leave a Comment