Java – string array mismatch

My program shows a series of questions (one by one) After I have written my answer, a warning message should tell me whether the answer is right or wrong

final String questions[] = {"Who's Tom?","Who's Luca?","Who's Flavie?"}
final String answers[] = {"American","Italian","French"}

// display question
answer_question.setOnClickListener(new View.OnClickListener() {
    int CurrentQuestionIndex = 0;

    public void onClick(View v) {
        ask_question.setText(question[(CurrentQuestionIndex++) % (questions.length)]);

        // discuss question versus answer
        EditText answer = (EditText) findViewById(R.id.tvReponseF);

        if (answer.equals(answers[CurrentQuestionIndex])) {
            alertMessageRight();
        } else {
            alertMessageFalse();
        }
    } 
});

Solution

The problem is that you are comparing the EditText object with a string field You must compare string with string

How is this done:

String answer = ((EditText) findViewById(R.id.tvReponseF)).getText().toString();

if(answer.equals(answers[CurrentQuestionIndex]))
{
     ...
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>