我有一个Recyclerview,每行有两个单选按钮。一个是'是'而另一个是'否'。我想一次只从每行中选择一个单选按钮,并在数组列表中获取该单选按钮的值或位置。为了实现单一选择,我在Radio组中包含了这两个单选按钮。我需要在默认情况下将每行中的“否”单选按钮设置为活动状态。之后,用户可以选择“是”或“否”,并且所选值应存储在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>
答案 0 :(得分:0)
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.
}