CHarris
CHarris

Reputation: 2793

How do I make button select all checkboxes in my recyclerView?

The result of my code is that all the checkboxes are checked only after I scroll down past the checkboxes in the recyclerView - when I scroll back up, they are checked.

How can I have them checked as soon as I click the button?

In my activity I have:

    private void publicButton() {

        publicContacts.setOnClickListener(new View.OnClickListener() {

 @Override
        public void onClick(View v) {

            //loop through the contacts
            int count = PopulistoContactsAdapter.theContactsList.size();

            for (int i = 0; i < count; i++) {
                PopulistoContactsAdapter.theContactsList.get(i).setSelected(true);
                }
        }
    });
}

And for the recyclerViewAdapter:

@Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        //bind the views into the ViewHolder
        //selectPhoneContact is an instance of the SelectPhoneContact class.
        //We will assign each row of the recyclerview to contain details of selectPhoneContact:

        //The number of rows will match the number of phone contacts
        final SelectPhoneContact selectPhoneContact = theContactsList.get(position);

        //if the row is a matching contact
        if (viewHolder.getItemViewType() == 1)

        {
            //in the title textbox in the row, put the corresponding name etc...
            ((MatchingContact) viewHolder).title.setText(selectPhoneContact.getName());
            ((MatchingContact) viewHolder).phone.setText(selectPhoneContact.getPhone());
            //((MatchingContact) viewHolder).check.setText("Cheeckbox" + position);
            ((MatchingContact) viewHolder).check.setChecked(theContactsList.get(position).isSelected);
            ((MatchingContact) viewHolder).check.setTag(position);

            ((MatchingContact) viewHolder).check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //pos is the row number that the clicked checkbox exists in
                    Integer pos = (Integer) ((MatchingContact) viewHolder).check.getTag();

                    Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " clicked!", Toast.LENGTH_SHORT).show();

                    if (theContactsList.get(pos).getSelected()) {
                        theContactsList.get(pos).setSelected(false);
                    } else {
                        theContactsList.get(pos).setSelected(true);
                    }
                }
            });

            }
    }

The onClick in the Adapter works as planned for individual checkboxes; their state is preserved. It's just when I try to select all of them with the button in the activity that the problem arises.

Upvotes: 0

Views: 766

Answers (1)

Ben P.
Ben P.

Reputation: 54214

The simplest answer by far is to simply call notifyDataSetChanged() on your adapter after you make all of your setSelected() calls.

From your code, I can't tell if PopulistoContactsAdapter is an adapter instance, or if PopulistoContactsAdapter.theContactsList is a static variable... you'll need an adapter instance to call notifyDataSetChanged(). If you only have a reference to the RecyclerView, you can call myRecyclerView.getAdapter().notifyDataSetChanged().

Upvotes: 1

Related Questions