I am currently doing a quiz app and the question should be coming out based on the answer. The below is my code. I have been looking online to try various options and cannot find a way to resolve it without making lots of errors. The code for my quiz activity is below. Thanks.
quiz.java
public class quiz extends AppCompatActivity implements View.OnClickListener {
TextView questionNo, question;
RadioGroup rg;
Button quiz_nextBtn, quiz_PrevBtn;
RadioButton rb1, rb2;
int current_question = 0;
int next_question=0;
private quizAdapter[] questionBank = new quizAdapter[]{
new quizAdapter(R.string.q1, R.string.q1_a1, R.string.q1_a2),
new quizAdapter(R.string.q2, R.string.q2_a1, R.string.q2_a2),
new quizAdapter(R.string.q3, R.string.q3_a1, R.string.q3_a2),
new quizAdapter(R.string.q4, R.string.q4_a1, R.string.q4_a2),
new quizAdapter(R.string.q5, R.string.q5_a1, R.string.q5_a2),
new quizAdapter(R.string.q6, R.string.q6_a1, R.string.q6_a2),
new quizAdapter(R.string.q7, R.string.q7_a1, R.string.q7_a2)
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
rg = findViewById(R.id.quiz_answer);
rb1 = findViewById(R.id.rb1);
rb2 = findViewById(R.id.rb2);
questionNo = findViewById(R.id.quiz_questionNo);
question = findViewById(R.id.quiz_question);
quiz_nextBtn = findViewById(R.id.quiz_nextBtn);
quiz_PrevBtn = findViewById(R.id.quiz_PrevBtn);
quiz_nextBtn.setOnClickListener(this);
quiz_PrevBtn.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.quiz_nextBtn:
if(current_question<7){
current_question++;
if(current_question == 6){
question.setText("We are finish");
}
else {
if(current_question == 0 && rb1.isChecked()){
current_question = 1;
}
else if (current_question == 0 && rb2.isChecked()){
current_question = 4;
}
else if (current_question == 1 && rb1.isChecked()){
current_question = 2;
}
else if (current_question == 1 && rb2.isChecked()){
current_question = 3;
}
else if (current_question == 4 && rb1.isChecked()){
current_question = 5;
}
else if (current_question == 4 && rb2.isChecked()){
current_question = 6;
}
updateQuestion();
}
}
break;
case R.id.quiz_PrevBtn:
if (current_question > 0) {
current_question = (current_question - 1) % questionBank.length;
updateQuestion();
}
}
}
private void updateQuestion() {
Log.d("Current", "onClick: " + current_question);
question.setText(questionBank[current_question].getQuestion());
rb1.setText(questionBank[current_question].getRb1());
rb2.setText(questionBank[current_question].getRb2());
}
}
quizAdapter.java
public class quizAdapter {
private int question;
private int rb1;
private int rb2;
public quizAdapter(int question, int rb1, int rb2)
{
this.question = question;
this.rb1 = rb1;
this.rb2 = rb2;
}
public int getQuestion()
{
return question;
}
public int getRb1()
{
return rb1;
}
public int getRb2()
{
return rb2;
}
}
question from:
https://stackoverflow.com/questions/65871541/android-quiz-how-to-show-the-question-based-on-the-answer 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…