简体   繁体   中英

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. 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. For some reason it most of the time uses the original text of TextView as ''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 tvAnswer = (TextView) findViewById(R.id.tvAnswer);
String correctAnswer = "Correct Answer";
if(correctAnswer.equals(tvAnswer.getText.toString()) )
 {
   //do something
 }
 else{
   //do something
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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