1
I’m trying to make a RecyclerView
, where each recycleritem has 3 options of radio button.
Each of the radio butttons is related to an item in the list of objects that was passed to Adapter as parameter.
How do I get the selection of radio button selected? I have to do something with the radios Buttons in the method onBindViewHolder
?
Below, follow my Adapter:
public class LikeListAdapter extends RecyclerView.Adapter<LikeListAdapter.LikeItemViewHolder> {
private List<Goals> goalsList;
public LikeListAdapter(List<Goals> goalsList) {
this.goalsList = goalsList;
}
@Override
public LikeItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View view = inflater.inflate(R.layout.recycler_item_like, parent, false);
return new LikeItemViewHolder(view);
}
@Override
public void onBindViewHolder(LikeItemViewHolder holder, int position) {
Goals goals = goalsList.get(position);
if (goals != null && getItemCount() > 0) {
holder.goalsDescriptionTextView.setText(goals.getDescription());
}
}
@Override
public int getItemCount() {
return goalsList.size();
}
public class LikeItemViewHolder extends RecyclerView.ViewHolder implements RadioGroup.OnCheckedChangeListener {
@Bind(R.id.description_goalsTextView)
TextView goalsDescriptionTextView;
@Bind(R.id.happy_radio)
RadioButton happyRadioButton;
@Bind(R.id.soso_radio)
RadioButton sosoRadioButton;
@Bind(R.id.angry_radio)
RadioButton angryRadioButton;
@Bind(R.id.radio_like)
RadioGroup radioLike;
public LikeItemViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
radioLike.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.e("group","" + group);
Log.e("checkedId","" + checkedId);
switch (group.getId()) {
case (R.id.happy_radio):
Log.e("aqui", "happy");
break;
case (R.id.soso_radio):
Log.e("aqui", "soso");
break;
case (R.id.angry_radio):
Log.e("aqui", "angry");
break;
}
}
}
}
At what point do you want to take it? I believe you can use the
LayoutManager
to recover theViewHolder
and seek the value ofRadioButton
which is marked. The methodonBindViewHolder
is only to popular model values inViews
of the list.– Wakim
I would like to take the moment I select one of the radio button options.
– Lili
My suggestion is to register the
Adapter
like theListener
of the event, or the event itselfViewHolder
with a reference to the item model, and in this case update the model that is associated with the line instead of wanting to retrieve the value from the outside.– Wakim
I’m sorry if I’ve been too Umb, but how do I do it?
– Lili
I can come up with an answer, but it might take a little while to close.
– Wakim