I have a recyclerview with two radio buttons in every row. One is for 'yes' and another for 'No'. I want to select only one radio button from each row at a time and get the value or the position of that radio button in an array list. In order to achieve single selection, I have enclosed these two radio buttons inside a Radio group. I need to set 'No' radio button in every row as active by default. Later, the user can select either 'Yes' or 'No' and the selected values should be stored in an ArrayList. How I achieve this.
here is my adapter code.
public class MyHealthInfoAdapter extends RecyclerView.Adapter<MyHealthInfoAdapter.MyHealthViewHolder> {
private Context context;
private ArrayList<DonorHealthStatusQuestionare> listQuestions;
private RadioButton lastCheckedRadioBtn = null;
private ArrayList<Integer> listYes = new ArrayList<>();
private ArrayList<Integer> listNo = new ArrayList<>();
private FloatingActionButton floatingActionButton;
public MyHealthInfoAdapter() {
}
public MyHealthInfoAdapter(Context context, ArrayList<DonorHealthStatusQuestionare> listQuestions) {
this.context = context;
this.listQuestions = listQuestions;
}
@Override
public MyHealthViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_my_health_info, parent, false);
return new MyHealthViewHolder(view);
}
@Override
public void onBindViewHolder(final MyHealthViewHolder holder, final int position) {
final DonorHealthStatusQuestionare donorHealthStatusQuestionare = listQuestions.get(position);
holder.tvQuestion.setText(donorHealthStatusQuestionare.getQuestions());
}
@Override
public int getItemCount() {
return listQuestions.size();
}
public class MyHealthViewHolder extends RecyclerView.ViewHolder {
CardView cvParent;
TextView tvQuestion;
RadioButton radioYes, radioNo;
RadioGroup radioGroup;
public MyHealthViewHolder(View itemView) {
super(itemView);
cvParent = itemView.findViewById(R.id.card_view_my_health_status_rv);
tvQuestion = itemView.findViewById(R.id.tv_eligibility_question_my_info);
radioYes = itemView.findViewById(R.id.radio_yes_my_health_info);
radioNo = itemView.findViewById(R.id.radio_no_my_health_info);
radioGroup = itemView.findViewById(R.id.my_health_status_check_group);
}
}
}
And here is my recyclerview layout.
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view_my_health_status_rv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="12dp">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1">
<TextView
android:id="@+id/tv_eligibility_question_my_info"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<RadioGroup
android:orientation="horizontal"
android:id="@+id/my_health_status_check_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0">
<RadioButton
android:id="@+id/radio_yes_my_health_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="@string/yes" />
<RadioButton
android:id="@+id/radio_no_my_health_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no" />
</RadioGroup>
</LinearLayout>