Mehmet Mehmet - 2 months ago 7
Android Question

Android Questions App Answer==MyAnswer not null

I make android questions app. I can not find the right answer. Answer list.

Right answer: String answer = quizList.get(position).getAnswer();

Button Click:

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



if (answer =="A"){

lblsignboard.setText("Ok");

}
else
{

lblsignboard.setText("No");


}
}
});


Answer = "A", but this code does not work. Always gives the answer is no.

Answer

You can use this way too, to keep you code working even the answer key is of different case. Use either equals("A") or equalsIgnoreCase("a").

 buttonA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer.equals("A") || equalsIgnoreCase("a")){
                lblsignboard.setText("Ok");
            }else{
                lblsignboard.setText("No");
            }
        }
    });