hect1c
hect1c

Reputation: 31

Get all items from adapter (recyclerview) in Fragment

Hello I'm having an issue getting all of the items from my adapter in my fragment. To be more specific I am using a ScaleInAnimatorAdapter along with my Customer Adapter and when I attempt to get my checkbox items from the below posted code, within my Fragment, I only seem to get the visible items on screen.

private View.OnClickListener onAllClick = new View.OnClickListener() {
    @Override public void onClick(View v) {
        int count = listAdapter.getItemCount();

        for (int i = 0; i < count; i++) {
            View mChild = listTopics.getChildAt(i);

            if( mChild != null ) {
                Log.d(TAG,"getItemCount(): " + i );

                CheckBox cBox = (CheckBox)mChild.findViewById(R.id.topic_chk);
                cBox.setChecked(((CheckBox) v).isChecked());
                Log.d(TAG,"isChecked" + cBox.getTag());
                cBox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(((CheckBox) v).isChecked()) {
                            checkboxAll.setChecked(false);
                        }
                    }
                });
            }
        }
    }
};

Essentially I am attempting to create a check all feature from the fragment, so that when this is clicked all of the checkbox items from the adapter are checked. I got that to work with the code I presented however the main issue is that I only get the items visible on the screen, so when I scroll to my other items they are not checked. Thus I am wondering if there is a better way or another way for me to get all of the items

Upvotes: 2

Views: 10945

Answers (4)

hect1c
hect1c

Reputation: 31

Ok. Thank you all for those who have replied you helped me grasp something really basic and important that had alluded me.

I got it to work, so for anyone who might read this post, here is my solution:

  • Create necessary methods and field to update your data in a model (ie, private is_checked, set_checked(), is_checked(), etc)
  • In your adapter onBindViewHolder, you will set your holder checkbox to be associated to the data so something like:

    holder.checkBox.setOnCheckedChangeListener(new  CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            obj.setSelected(buttonView.isChecked());
        }
    });
    holder.checkBox.setChecked(obj.isSelected());
    
  • Then in your fragment or activity I just have a click listener on my main checkbox, and then when checked I loop through my data list, and using the model method I update my data and just simply use listAdapter.notifyDataSetChanged()

Upvotes: 1

Alok
Alok

Reputation: 881

You are doing it wrongly. You should update the adapter instead of updating the check box view.

What you can do , you can create a field in adapter data holder .

And whenever you change the selection,just update the field or multiple field and finally refresh the view.

Upvotes: 0

Udayaditya Barua
Udayaditya Barua

Reputation: 1162

Basically you can't. It'll be reusing the UI views. You need to set a flag in all your data list objects, call notifyDataSetChanged() and onBindViewHolder check that flag and use it to check uncheck

if(listAdapter.getItem(position).getIsChecked())
    viewHolder.checkBox.setChecked(true);
else
   viewHolder.checkBox.setChecked(false);

Upvotes: 1

Jon Adams
Jon Adams

Reputation: 25137

Adapters are meant to bind underlying data stores to views; they generally shouldn't be used to store data themselves (except for having a copy of the data for view binding purposes) nor should they perform actions on data.

Instead, you should be modifying the underlying data, then updating the adapter through whatever mechanism you are already using. (Loaders, custom setters with notifyDataItemChanged, etc.).

Upvotes: 2

Related Questions