Skip to content Skip to sidebar Skip to footer

How To Update Data In Recyclerview Item From Another Item?

I have a problem with RecyclerView, I have RecyclerView which has a radio button and few other views in each row item, What I wanted exactly is, when a RadioButton is checked by us

Solution 1:

You need to have a reference to the selectedItem globally in your adapter and then update all the items when the user checks a new radio button.

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    private int selectedItemPosition = -1;
}

And do things in your onBindViewHolder() method of the adapter,

@Override
public void onBindViewHolder(final Adapter.ViewHolder holder, int position) {
    holder.radioButton.setChecked(holder.getAdapterPosition()==selectedItemPosition);
    holder.radioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectedItemPosition = holder.getAdapterPosition();
            notifyDataSetChanged();
        }
    });
}

Solution 2:

Try this::

        if (position == selectedPosition) {
            holder.radioButton.setChecked(true);
            Toast.makeText(activity, "section: " + selectedPosition, Toast.LENGTH_SHORT).show();
        } else {
            holder.radioButton.setChecked(false);
        }

        holder.radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition = holder.getAdapterPosition();
                notifyDataSetChanged();
            }
        });

Post a Comment for "How To Update Data In Recyclerview Item From Another Item?"