select 如何在 android 的 RecyclerView 中 select radiogroup 中的一个单选按钮?

How to select one radio button from radiogroup in a RecylcerView in android?

所以我想创建这个 activity,其中有 10 个问题,每个问题有 5 个答案。
像这样 - UI recyclerview

我想获得用户为每个问题选择的答案,例如 q01 - 4 和 q02 - 2 等。我是开发新手,所以我不知道该怎么做?谁能帮帮我?

这是我的适配器类,

public class DailyQuestionAdapter extends RecyclerView.Adapter<DailyQuestionAdapter.ViewHolder> {
 private ArrayList<DailyQuestionModel> questionList;

public DailyQuestionAdapter(ArrayList<DailyQuestionModel> questionList){
    this.questionList = questionList;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View questionView = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_daily, parent, false);
    return new ViewHolder(questionView);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String question = questionList.get(position).getQuestion();
    holder.questionText.setText(question);
}

@Override
public int getItemCount() {
    return questionList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public TextView questionText;
    public RadioGroup answers;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        questionText = itemView.findViewById(R.id.daily_question_textview);
        answers = itemView.findViewById(R.id.daily_answer);

    }
}

这是我的主要 Class,

public class DailyQuestionsActivity extends AppCompatActivity {
 private ArrayList<DailyQuestionModel> questionList;
 private RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily_questions);

    recyclerView = findViewById(R.id.daily_question_recycler);
    questionList = new ArrayList<>();

    setQuestion();
    setAdapter();
}

private void setAdapter() {
    DailyQuestionAdapter adapter = new DailyQuestionAdapter(questionList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(adapter);
}

private void setQuestion() {
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q01)));
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q02)));
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q03)));
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q04)));
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q05)));
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q06)));
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q07)));
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q08)));
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q09)));
    questionList.add(new DailyQuestionModel(getResources().getString(R.string.q10)));
}
}

提前致谢!

使用接口作为单选按钮点击的监听器。

public interface AnswerSelectedListener {
    void answerSelected(String answer);
}

将 AnswerSelectedListener class 属性添加到您的 DailyQuestionModel class 和构造函数并将此接口的实现传递给您的每个 DailyQuestionModel:

questionList.add(new DailyQuestionModel(
    getResources().getString(R.string.q01),
    new AnswerSelectedListener() {
        @Override
        public void answerSelected(String answer) {
            //Here you have the selected answer to question 01
        }
    })
)

在您的 RecyclerView 适配器内部,选择答案时调用此侦听器:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    final DailyQuestionModel model = questionList.get(position);
    String question = model.getQuestion();
    holder.questionText.setText(question);
    answers.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
            boolean isChecked = checkedRadioButton.isChecked();
            if (isChecked) {
                model.answerListener.answerSelected(checkedRadioButton.getText()))
            }
        }
    });
}

此代码未经测试,但我认为它给出了整体流程。