Android应用不想在刷新信息时更新信息

时间:2018-10-25 20:57:42

标签: android

首先,对不起,我无法真正正确地构造问题,但基本上,我是android开发的新手,我练习了一个测验游戏。该应用程序包含一个带有Questions的类和一个QuestionRepository类,其中本身包含一个包含问题的arrayList。

问题课              包com.program.uni.quizgame;              导入java.util.Random;

  public class Questions {
     private String question;
     private String answer1;
     private String answer2;
     private String answer3;
  private String answer4;
  private int winningQuestion;


public Questions(String question, String answer1, String answer2, String answer3, String answer4, int winningQuestion) {
    this.question = question;
    this.answer1 = answer1;
    this.answer2 = answer2;
    this.answer3 = answer3;
    this.answer4 = answer4;
    this.winningQuestion = winningQuestion;
}

public String getQuestion() {
    return question;
}

public void setQuestion(String question) {
    this.question = question;
}

public String getAnswer1() {
    return answer1;
}

public void setAnswer1(String answer1) {
    this.answer1 = answer1;
}

public String getAnswer2() {
    return answer2;
}

public void setAnswer2(String answer2) {
    this.answer2 = answer2;
}

public String getAnswer3() {
    return answer3;
}

public void setAnswer3(String answer3) {
    this.answer3 = answer3;
}

public String getAnswer4() {
    return answer4;
}

public void setAnswer4(String answer4) {
    this.answer4 = answer4;
}

public int getWinningQuestion() {
    return winningQuestion;
}

public void setWinningQuestion(int winningQuestion) {
    this.winningQuestion = winningQuestion;
}

@Override
public String toString() {
    return question;
}

}

问题库类

  import java.util.Random;
   import java.util.ArrayList;

 public class QuestionRepository {
     private Random randomGenerator;
      ArrayList<Questions>  myQuestions;

public QuestionRepository(ArrayList<Questions> myQuestions) {
    this.myQuestions = myQuestions;
    randomGenerator = new Random();
}

public Questions getRandomQuestion(){
    int index = randomGenerator.nextInt(myQuestions.size());
    Questions questions = myQuestions.get(index);
    return questions;
}

}

最后我的主要活动。请原谅硬编码部分。      包com.program.uni.quizgame;

 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;

   import org.w3c.dom.Text;

     import java.util.ArrayList;

 public class MainActivity extends AppCompatActivity {
     private Button button1;
     private Button button2;
     private Button button3;
     private Button button4;
     private QuestionRepository questionRepository;
     private TextView textView;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       ArrayList<Questions> myArrayslist = new ArrayList<Questions>();


    myArrayslist.add(new Questions("What's 2+2? ", "4", "2", "1", "69", 4));
    myArrayslist.add(new Questions("What's 2+1? ", "1/2", "1", "4", "3", 3));
    myArrayslist.add(new Questions("What's 3+2? ", "0", "5", "6", "7", 5));

     questionRepository = new QuestionRepository(myArrayslist);
    textView = (TextView) findViewById(R.id.textForQuestions);
    Questions randomQuestion = questionRepository.getRandomQuestion();
    textView.setText(randomQuestion.getQuestion());
     button1 = (Button) findViewById(R.id.button1);
     button2 = (Button) findViewById(R.id.button2);
     button3 = (Button) findViewById(R.id.button3);
     button4 = (Button) findViewById(R.id.button4);
    button1.setText(randomQuestion.getAnswer1());
    button2.setText(randomQuestion.getAnswer2());
    button3.setText(randomQuestion.getAnswer3());
    button4.setText(randomQuestion.getAnswer4());
    int answerToConvert = questionRepository.getRandomQuestion().getWinningQuestion();
     String convertedAnswer = Integer.toString(answerToConvert);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                   if(!button1.getText().equals(convertedAnswer)){
                       button1.setEnabled(false);
                   } else if(button1.getText().equals(convertedAnswer)){
                       textView.setText("Congratulations, the answer is: " + convertedAnswer);
                   }
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if(!button2.getText().equals(convertedAnswer)){
                button2.setEnabled(false);
            } else if(button2.getText().equals(convertedAnswer)){
                textView.setText("Congratulations, the answer is: " + convertedAnswer);
            }
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!button3.getText().equals(convertedAnswer)){
                button3.setEnabled(false);
            } else if(button3.getText().equals(convertedAnswer)){
                textView.setText("Congratulations, the answer is: " + convertedAnswer);
            }
        }
    });

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!button4.getText().equals(convertedAnswer)){
                button4.setEnabled(false);
            } else if(button4.getText().equals(convertedAnswer)){
                textView.setText("Congratulations, the answer is: " + convertedAnswer);
            }
        }
    });










}

}

所以我想得到一个特定问题的答案,就我而言,这是一个整数,但是我将其转换为String并将该String与getText()进行比较;按钮,我之前已设置为代表每个答案。如果答案相同,我将做一些事情,否则,我将把按钮涂黑。

我收到一个错误消息,说要在OnclickListener中使用 convertedAnswer ,必须先将其定为最终版本。我该如何解决这个问题。任何帮助,将不胜感激。由于我是新来的,请随时为我的糟糕编码而抨击我。预先谢谢你!

2 个答案:

答案 0 :(得分:0)

您应该可以将convertedAnswer标记为final

final String convertedAnswer = Integer.toString(answerToConvert);

答案 1 :(得分:0)

j设置为字段变量,并且不要将其设为最终变量,这样可以解决您的问题。

public class MainActivity extends AppCompatActivity {
     private Button button1;
     private Button button2;
     private Button button3;
     private Button button4;
     private QuestionRepository questionRepository;
     private TextView textView;
     String convertedAnswer; //define it in here.
     @Override
     protected void onCreate(Bundle savedInstanceState) {