我正在尝试制作一个多项选择应用程序。我有一个对象列表,其中包含所有必要的信息(问题、四个答案和一个正确的答案)。四个按钮以文本形式分配了四个答案。
当用户单击按钮时,应用程序会检查所选答案是正确还是错误。我需要将下一组信息加载到相应的组件中。
尝试在 onClick
方法内调用 loadQuestionElements
,但没有产生请求的结果。
主要 Activity
FetchQ fetchq = new FetchQ();
List<Question> questions = null;
String correctAnswer;
private Bundle savedInstanceState;
int iterator = 0;
Question question;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.savedInstanceState = savedInstanceState;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tQuestion = findViewById(R.id.textViewQuestion);
Button answer1 = findViewById(R.id.bAnswer1);
Button answer2 = findViewById(R.id.bAnswer2);
Button answer3 = findViewById(R.id.bAnswer3);
Button answer4 = findViewById(R.id.bAnswer4);
answer1.setOnClickListener(this);
answer2.setOnClickListener(this);
answer3.setOnClickListener(this);
answer4.setOnClickListener(this);
//Retrieves all the questions from the database
try {
questions = fetchq.execute().get();
Collections.shuffle(questions);
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
this.question = loadQuestionElements(questions, iterator, tQuestion, answer1, answer2, answer3, answer4);
this.correctAnswer = question.getCorrectAnswer();
}
这就是我将信息加载到各个组件的方式
public Question loadQuestionElements(List<Question> questions, int iterator, TextView question, Button option1, Button option2, Button option3, Button option4){
Question q = questions.get(iterator);
question.setText(q.getQuestion());
option1.setText(q.getOption1());
option2.setText(q.getOption2());
option3.setText(q.getOption3());
option4.setText(q.getOption4());
this.correctAnswer = q.getCorrectAnswer();
this.iterator++;
return q;
}
不知道是否有帮助,但这是我的 onClick 方法
@Override
public void onClick(View v) {
Button button = (Button) v;
switch(v.getId()){
case R.id.bAnswer1:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}
case R.id.bAnswer2:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}
case R.id.bAnswer3:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}
case R.id.bAnswer4:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}
}
}
最佳答案
创建一个新方法并在每个 switch case 语句后调用此方法
private void getNewQuestion(){
this.question = loadQuestionElements(questions, iterator, tQuestion, answer1, answer2, answer3, answer4);
this.correctAnswer = question.getCorrectAnswer();
}
现在对于 switch case,在每个 case 之后,您必须加载新问题
switch(v.getId()){
case R.id.bAnswer1:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}
getNewQuestion();
break;
case R.id.bAnswer2:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}
getNewQuestion();
break;
case R.id.bAnswer3:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}
getNewQuestion();
break;
case R.id.bAnswer4:
if (this.question.isCorrect(button.getText().toString(),this.correctAnswer)){
button.setBackgroundColor(0xFF4CAF50);//Set button background color to green
}else{
button.setBackgroundColor(0xFFF44336);//Set button background color to red
}
getNewQuestion();
break;
}
关于java - 单击按钮后如何加载下一组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58362217/