UI不会自动更新,只会在刷新后更改

时间:2017-09-07 02:45:48

标签: java android

我刚开始使用Android应用并进行测验应用,用户必须完成测验才能获得分数。但我无法为setText更新setImageResultActivity。当我在QuizActivity完成测验时,ResultActivity仅更新分数,但不会更新我想要的屏幕文字和图像。

以下是代码,我可以基于“得分”setText,但我必须刷新用户界面以查看更改。我从另一个活动传递分数并根据分数更改文本。请帮助我,提前谢谢。

//code for QuizActivity      
private void updateQuestion(){
    if(mQuestionNumber < mQuestionLibrary.getLength()){
        mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
        mButtonChoice1.setText(mQuestionLibrary.getChoice(mQuestionNumber, 1));
        mButtonChoice2.setText(mQuestionLibrary.getChoice(mQuestionNumber, 2));
        mButtonChoice3.setText(mQuestionLibrary.getChoice(mQuestionNumber, 3));
        mButtonChoice4.setText(mQuestionLibrary.getChoice(mQuestionNumber,4));
        mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
        mQuestionNumber++;
    }
    else {
        Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
        intent.putExtra("score", mScore);
        // pass the current score to the second screen
        startActivity(intent);

        //The Following is the code for ResultActivity
        Intent intent = getIntent();
        int score = intent.getIntExtra("score", 0);
        SharedPreferences mypref = getPreferences(MODE_PRIVATE);
        int highscore = mypref.getInt("highscore", 0);
        if (highscore < score) {
            txtScore.setText("Your Score: " + score + " /10");
            SharedPreferences.Editor editor = mypref.edit();
            editor.putInt("highscore", score);
            editor.commit();
        }
        else {
            int score1 = score + highscore;
            txtScore.setText(" Your score: " + score1 + " /10");
            SharedPreferences.Editor editor1 = mypref.edit();
            editor1.putInt("highscore", score1);
            editor1.commit();
        }
        if (highscore == 0) {
            image1.setVisibility(View.GONE);
            txtScore.setVisibility(View.GONE);
            resulttext.setText("INSTRUCTION \n" +
                "\n" +
                "1) CLICK on ‘hello!’\n" +
                "\n" +
                "2) VISIT hello\n" +
                "\n" +
                "3)EE" +
                "\n" +
                "4)EE!");
            btnscan.setText("abc!");
        }
        else if (highscore == 1 & highscore < 2) {
            resulttext.setText("Well Done!");
            image1.setImageResource(R.drawable.bubble);
            btnscan.setText("Let's bubble on");
        }
        else if (highscore >= 3) {
            resulttext.setText("These are good practices to learn.\n" +
                "\n" +
                "Bloop bloop~");
            image1.setImageResource(R.drawable.lightbulb);
            btnscan.setText("Let’s keep swimming");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

从您的代码中,您尝试使用以下命令启动ResultActivity:

Intent intent = new Intent(QuizActivity.this, ResultActivity.class);
intent.putExtra("score", mScore);
// pass the current score to the second screen
startActivity(intent);

然后你想用代码更新活动:

//The Following is the code for ResultActivity
Intent intent = getIntent();
int score = intent.getIntExtra("score", 0);
SharedPreferences mypref = getPreferences(MODE_PRIVATE);
...

期望在显示活动后执行代码。

但这不会发生,因为startActivity异步方法。因此,您需要在启动ResultActivity之前更新数据或更新ResultActivity中的数据。