简体   繁体   English

比较TextView String时equals方法不起作用

[英]equals method doesn't work when comparing TextView String

So I'm new to java and I'm trying to make quiz app as exercise. 所以我是Java的新手,我正在尝试使测验应用程序成为练习。 I kinda made one but it doesn't work: 我有点做,但不起作用:

public class QuizActivity extends AppCompatActivity {
TextView QuestionText;
Button button1;
Button button2;
Button button3;
Button button4;
ArrayList<Question> listOfQuestions;
int currentQuestion = 0;
Context context = this;
int NumberOfQuestions;
GameCreator game;
String totalCorrect = "";
String totalWrong = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    QuestionText = (TextView) findViewById(R.id.textJautajums);
    button1 = (Button) findViewById(R.id.buttonOpcija1);
    button2 = (Button) findViewById(R.id.buttonOpcija2);
    button3 = (Button) findViewById(R.id.buttonOpcija3);
    button4 = (Button) findViewById(R.id.buttonOpcija4);
    NumberOfQuestions = Integer.parseInt(context.getString(R.string.JautajumuSkaits).toString());
    game = new GameCreator(NumberOfQuestions);
    listOfQuestions = game.makeQuestions();

    Resources r = getResources();
    int px1 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 165, r.getDisplayMetrics());
    button1.setWidth(px1);
    button2.setWidth(px1);
    button3.setWidth(px1);
    button4.setWidth(px1);

    Resources e = getResources();
    int px2 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 125, r.getDisplayMetrics());
    button1.setHeight(px2);
    button2.setHeight(px2);
    button3.setHeight(px2);
    button4.setHeight(px2);


    if (currentQuestion == 0){
        setQuestion(listOfQuestions.get(0));
    }

    button1.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View V){
                    gajiens(button1.getText().toString(), listOfQuestions.get(currentQuestion));
                    currentQuestion++;
                }
            }
    );

    button2.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View V){
                    gajiens(button2.getText().toString(), listOfQuestions.get(currentQuestion));
                    currentQuestion++;
                }
            }
    );

    button3.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View V){
                    gajiens(button3.getText().toString(), listOfQuestions.get(currentQuestion));
                    currentQuestion++;
                }
            }
    );

    button4.setOnClickListener(
            new Button.OnClickListener(){
                public void onClick(View V){
                    gajiens(button4.getText().toString(), listOfQuestions.get(currentQuestion));
                    currentQuestion++;
                }
            }
    );


}

public void gajiens(String answer, Question thisQuestion){
    if (currentQuestion < 14){

        if (answer.equals(thisQuestion.getAnswer())){
            totalCorrect += "Question: " + thisQuestion.getQuestion() + "\nYour Answer: " + answer + "\n";
        } else {
            totalWrong += "Question: " + thisQuestion.getQuestion()) + "\nYour Answer: " + answer + "\n";
        }

        currentQuestion++;
        setQuestion(listOfQuestions.get(currentQuestion));
    } else {
        Intent intent = new Intent(this, EndActivity.class);
        intent.putExtra("correct", totalCorrect);
        intent.putExtra("wrong", totalWrong);
        startActivity(intent);
    }
}

public void setQuestion(Question kursh){
    QuestionText.setText(kursh.getJautajums());
    button1.setText(kursh.getOption1());
    button2.setText(kursh.getOption2());
    button3.setText(kursh.getOption3());
    button4.setText(kursh.getOption4());
}

object Question is: 对象问题是:

public Question(String Question, String Option1, String Option2, String Option3,String Option4, String correctAnswer){
    Question = Question;
    Option1 = Option1;
    Option2 = Option2;
    Option3 = Option3;
    Option4 = Option4;
    correctAnswer = correctAnswer;
}

Basically the problem is that App doesn't count the right answers. 基本上,问题在于App无法计算正确答案。 For some reason it most of the time uses the original text of TextView as ''correctAnswer''. 由于某种原因,它大多数时候都将TextView的原始文本用作“ correctAnswer”。 Anyone has any idea what to do? 有人知道该怎么办吗? I suspect since this isn't working properly this isn't particularly best approach so maybe someone can suggest a better one? 我怀疑由于这无法正常工作,因此这并不是最佳方法,因此也许有人可以建议一个更好的方法?

to compare the value of a TextView with a String you can do this as below 可以将TextView的值与String进行比较,如下所示

TextView tvAnswer = (TextView) findViewById(R.id.tvAnswer);
String correctAnswer = "Correct Answer";
if(correctAnswer.equals(tvAnswer.getText.toString()) )
 {
   //do something
 }
 else{
   //do something
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM