Recyclerview with radio group select only yes or no
Recyclerview with radio group select only yes or no
我有一个 recyclerview,每行都有两个单选按钮。一个用于 'yes',另一个用于 'No'。我想一次从每一行 select 只选择一个单选按钮,并获取该单选按钮在数组列表中的值或位置。为了实现单个 selection,我将这两个单选按钮包含在一个单选组中。我需要将每一行的 'No' 单选按钮默认设置为活动状态。稍后,用户可以 select 'Yes' 或 'No' 并且 selected 值应存储在 ArrayList 中。我是如何实现的。
这是我的适配器代码。
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);
}
}
}
这是我的 recyclerview 布局。
<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>
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rbbutton=(RadioButton)findViewByID(checkedId);
if(rbbutton.getText().toString().equalsIgnoreCase("Yes")){
listYes.add(position)
}else{
listNo.add(position);
}
}
});
全部取消选中的代码段:
for(int i=0;i<listQuestions.size();i++){
//do your logic here.
}
我有一个 recyclerview,每行都有两个单选按钮。一个用于 'yes',另一个用于 'No'。我想一次从每一行 select 只选择一个单选按钮,并获取该单选按钮在数组列表中的值或位置。为了实现单个 selection,我将这两个单选按钮包含在一个单选组中。我需要将每一行的 'No' 单选按钮默认设置为活动状态。稍后,用户可以 select 'Yes' 或 'No' 并且 selected 值应存储在 ArrayList 中。我是如何实现的。
这是我的适配器代码。
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);
}
}
}
这是我的 recyclerview 布局。
<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>
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rbbutton=(RadioButton)findViewByID(checkedId);
if(rbbutton.getText().toString().equalsIgnoreCase("Yes")){
listYes.add(position)
}else{
listNo.add(position);
}
}
});
全部取消选中的代码段:
for(int i=0;i<listQuestions.size();i++){
//do your logic here.
}