简体   繁体   中英

Why doesn't the text on my buttons update in my Android App?

So I am working on developing an app in Android Studio.

At this stage of development, my app seems to be working fine except for the update of the text in my buttons.

The code for my app that should update the text on my buttons is below.

private void selectedAnswer(int answerSelection) {
    Question currentQuestion = selectQuestion();

    answer0button.setText(currentQuestion.answer0);
    answer1button.setText(currentQuestion.answer1);
    answer2button.setText(currentQuestion.answer2);
    answer3button.setText(currentQuestion.answer3);


        if (answerSelection == 0) {
            if (answerSelection == currentQuestion.correctAnswer - 1) {
                answer0button.setText(currentQuestion.answer0 + " ✔");
                addPoints();
            }
            else {
                answer0button.setText(currentQuestion.answer0 + " ✗");
            }
        }
        else if (answerSelection == 1) {
            if (answerSelection == currentQuestion.correctAnswer - 1) {
                answer1button.setText(currentQuestion.answer1 + " ✔");
                addPoints();
            }
            else {
                answer1button.setText(currentQuestion.answer1 + " ✗");
            }
        }
        else if (answerSelection == 2) {
            if (answerSelection == currentQuestion.correctAnswer - 1) {
                answer2button.setText(currentQuestion.answer2 + " ✔");
                addPoints();
            }
            else {
                answer2button.setText(currentQuestion.answer2 + " ✗");
            }
        }
        else if (answerSelection == 3) {
            if (answerSelection == currentQuestion.correctAnswer - 1) {
                answer3button.setText(currentQuestion.answer3 + " ✔");
                addPoints();
            }
            else {
                answer3button.setText(currentQuestion.answer3 + " ✗");
            }
        }
        nextQuestion();
}

However, the line nextQuesiton() stops the text from updating properly. Without this line, the code works as expect.

The line in questions executes the following method.

private void nextQuestion(){
    gameRound += 1;
    displayQuestion(selectQuestion());
}

This method, in turn, executes this method.

private void displayQuestion(Question question){
    questionTextView.setText(question.questionText);
    answer0button.setText(question.answer0);
    answer1button.setText(question.answer1);
    answer2button.setText(question.answer2);
    answer3button.setText(question.answer3);
}

It seems that possibly the lines in the selectedAnswer() method and the displayQuestion() method execute at the same time, but the displayQuestion() method overrides the lines in selectedAnswer().

Can anyone tell me what is causing my problem exactly, and how to fix it.

I think there is nothing wrong with your code, but the update of the buttons happens too fast and it changes to the second text before you can even realize that it changed to another text before that. I think that, for the desired behaviour, you should implement an async call that executes the "nextQuestion" after some milliseconds.

Something like:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        nextQuestion();
    }
}, 1000);

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