java - 单击按钮后如何加载下一组元素

标签 java android

我正在尝试制作一个多项选择应用程序。我有一个对象列表,其中包含所有必要的信息(问题、四个答案和一个正确的答案)。四个按钮以文本形式分配了四个答案。

当用户单击按钮时,应用程序会检查所选答案是正确还是错误。我需要将下一组信息加载到相应的组件中。

尝试在 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/

相关文章:

java - Eclipse 上有关 sun/misc/BASE64Encoder 的错误

android - SpannableString 中的 URLSpan

android - 单个 ListView 内的多个 ViewPagers 崩溃

java - Android Studio 多个与 Android SDK 相关的错误

java - 使用共享首选项将数据发送到另一个 Activity

Java float to double - 上限和下限?

java - 哪个类可以访问我的方法?

java - 是否使引用变量易变,使其所有字段在 java 中也易变?

java - 无法保存RelativeLayout的状态

android - 我们可以在陀螺仪给出的两点之间在android Canvas 上画线吗?