Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 单击按钮后如何加载下一组元素_Java_Android - Fatal编程技术网

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

Java 单击按钮后如何加载下一组元素,java,android,Java,Android,我正在尝试制作一个选择题应用程序。我有一个对象列表,其中包含所有必要的信息(问题、四个答案和一个正确答案)。四个按钮将四个答案指定为文本 当用户单击按钮时,应用程序会检查所选答案是否正确。我需要将下一组信息加载到各个组件 尝试在onClick方法内调用loadQuestionElements,但未生成请求的结果 主要活动 创建一个新方法,并在每个switch case语句之后调用此方法 private void getNewQuestion(){ this.question = load

我正在尝试制作一个选择题应用程序。我有一个
对象列表
,其中包含所有必要的信息(问题、四个答案和一个正确答案)。四个按钮将四个答案指定为文本

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

尝试在
onClick
方法内调用
loadQuestionElements
,但未生成请求的结果

主要活动


创建一个新方法,并在每个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;
   }

在从问题列表中获取新问题之前,请确保检查列表大小。问题q=问题.get(迭代器);否则,索引越界异常可能会发生。看起来并不是那么容易,或者我遗漏了一些东西。如果我实现了
getNewQuestion
,我将无法访问
onCreate
方法中的UI组件。将它们声明为全局私有TextView tQuestion;专用按钮应答器1、应答器2、应答器3、应答器4;你的问题还在吗?
 @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
            }
   }
}
private void getNewQuestion(){
    this.question = loadQuestionElements(questions, iterator, tQuestion, answer1, answer2, answer3, answer4);
    this.correctAnswer = question.getCorrectAnswer();
    }
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;
   }