2

I need to iterate through all the RadioGroups in my RecyclerView.

Code i tried:

for(int i=0;i<recyclerView.getAdapter().getItemCount();i++)
  {
     radioGroup = recyclerView.findViewHolderForAdapterPosition(i).itemView.findViewById(R.id.radio_group);
  }

but findViewHolderForAdapterPosition(i) returns null for RadioGroups that are off screen and gives me a NullPointerException.

How do i iterate through all of them?

Nand gopal
  • 346
  • 3
  • 17

1 Answers1

4

RecyclerView views are created/destroyed as needed when you scroll. You cannot rely on them being available.

I assume you are trying to retrieve the selection state. It is better to store this within your object in the adapter and update its value upon clicking a radio button.

Then you can implement a getItem method in your adapter that returns the object and it's current selection state.

CodeChimp
  • 4,745
  • 6
  • 45
  • 61