gpt4 book ai didi

android - 检查 Recyclerview Adapter 中的单选按钮状态并从 MainActivity 对其应用更改

转载 作者:太空宇宙 更新时间:2023-11-03 12:26:29 28 4
gpt4 key购买 nike

在我的 MainActivity 方法中,checkAnswer 和 showSolution 必须修改,因为我将 RecycleView 适配器添加到项目并将所有 View 项移动到 QuestionAdapter。我不明白我应该如何在 QuestionAdapter 中使用 isChecked 检查 rbGroup 单选按钮的状态并将其 id 传递给 MainActivity。我试着检查一下:Radiogroup in recyclerview但我仍然不清楚下一步应该采取什么步骤。有人可以给我基本的指导,从现在开始修改我的项目应该采取什么步骤。仍然找不到在回收 View 中处理单选按钮列表的答案或教程。如何使用 MainActivity 的界面检查单选按钮状态?

按照建议进行了一些更新。不明白我应该如何修改 showSolution 方法。

public class MainActivity extends AppCompatActivity {

public QuestionAdapter adapter;
public ArrayList<Question> questionList;
private int questionCountTotal;
private long backPressedTime;
private int score;
private int questionCounter;
private Button btnConfirmNext;
private boolean answered;
private Question currentQuestion;
private TextView tvQuestion, tvScore, tvQuestionCount, tvCountdown;
public QuizDbHelper dbHelper;
RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnConfirmNext = findViewById(R.id.confirm_button);
tvCountdown = findViewById(R.id.count_down);
tvScore = findViewById(R.id.text_view_score);

recyclerView = findViewById(R.id.recycler_view);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.addItemDecoration(new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL));
questionList = new ArrayList<>();
adapter = new QuestionAdapter(this, questionList);

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 1);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);

dbHelper = new QuizDbHelper(this);
questionList = dbHelper.getAllQuestions();
questionCountTotal = questionList.size();
Collections.shuffle(questionList);

prepareQuestion();

btnConfirmNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkAnswer();
}
});

}


private void prepareQuestion() {
adapter = new QuestionAdapter(getApplicationContext(), questionList);
recyclerView.setAdapter(adapter);
if (questionCounter < questionCountTotal) {
currentQuestion = questionList.get(questionCounter);

answered = false;
btnConfirmNext.setText("Confirm");

} else {
finishQuiz();
}
}


//How should I handle it in onbindViewHolder

private void checkAnswer() {
answered = true;
countDownTimer.cancel();
RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
int answerNb = rbGroup.indexOfChild(rbSelected) + 1;

if (answerNb == currentQuestion.getAnswerNB()) {
score++;
tvScore.setText("Score: " + score);
}
showSolution();
}

//How should I change state of the items in recycle view

private void showSolution() {
rb1.setTextColor(Color.RED);
rb2.setTextColor(Color.RED);
rb3.setTextColor(Color.RED);
rb4.setTextColor(Color.RED);

switch (currentQuestion.getAnswerNB()) {
case 1:
rb1.setTextColor(Color.GREEN);
break;
case 2:
rb2.setTextColor(Color.GREEN);
break;
case 3:
rb3.setTextColor(Color.GREEN);
break;
case 4:
rb4.setTextColor(Color.GREEN);
break;
}

btnConfirmNext.setText("Finish");

}

问题适配器

public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.MyViewHolder> {

public ArrayList<Question> questionList;

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvQuestion, tvScore, tvQuestionCount, tvCountdown;
public RadioGroup rbGroup;
public RadioButton rb1, rb2, rb3, rb4;



public MyViewHolder(View view) {
super(view);
tvQuestion = view.findViewById(R.id.question);
rbGroup = view.findViewById(R.id.radiog_group);
rb1 = view.findViewById(R.id.radio_button1);
rb2 = view.findViewById(R.id.radio_button2);
rb3 = view.findViewById(R.id.radio_button3);
rb4 = view.findViewById(R.id.radio_button4);
}
}


public QuestionAdapter(Context mContext, ArrayList<Question> questionList) {
this.questionList = questionList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.question_card, parent, false);


return new MyViewHolder(itemView);
}

@NonNull
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final Question question = questionList.get(position);
holder.tvQuestion.setText(question.getQuestion());
holder.rb1.setText(question.getOption1());
holder.rb2.setText(question.getOption2());
holder.rb3.setText(question.getOption3());
holder.rb4.setText(question.getOption4());

holder.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {

// How can I handle Check status here and pass it to main activity?

}
});
}

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

更新。 在向我的 QuestionAdapter 添加接口(interface)并对构造函数和其他部分进行更改后,我在 MainActivity 中的 checkAnswer 看起来像这样

    private void checkAnswer() {
answered = true;
countDownTimer.cancel();
adapter = new QuestionAdapter(getApplicationContext(), questionList, new QuestionAdapter.OnItemListener() {
@Override
public void onItemSelect(int position) {
if (position+1==currentQuestion.getAnswerNB()){
score++;
tvScore.setText("Score: " + score);
}
}
});
showSolution();
}

现在我应该如何处理我的 showSolution 方法?似乎在 checkAnswer 之后我应该将信息发送回 QuestionAdapter 并在那里执行 setTextColor 。可能我走错了路......

MainActivity

最佳答案

你可以通过使用界面来做到这一点。

像这样创建一个接口(interface):

public interface OnItemListener {
public void onItemSelect(int position);
}

然后在您的适配器中像这样更改构造函数:

  QuestionAdapter(Context mContext, ArrayList<Question> questionList, OnItemListener 
onItemListener) {
this.onItemListener = onItemListener;
this.questionList = questionList;
}

并将这一行添加到您的 onCheckedChangeListener

            onItemListener.onItemSelect(position);

像这样:

holder.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {

// How can I handle Check status here and pass it to main activity?
onItemListener.onItemSelect(position);
}
});

然后在你的 Activity 中添加这个你定义适配器的地方(这是定义适配器的另一种方式,你也可以使用你自己的方式只记得像这样添加接口(interface)(新的 OnItemListener ...)):

 dbHelper = new QuizDbHelper(getApplicationContext(), questionList, new 
OnItemListener({

@Override
public void onItemSelect(int position) {
// handle your check state of adapter item here
}

});

关于android - 检查 Recyclerview Adapter 中的单选按钮状态并从 MainActivity 对其应用更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52520753/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com