2017-09-07 12 views
0

Androidアプリで作業を開始し、スコアを得るためにクイズを完了する必要があるクイズアプリを開始しました。しかしsetTextsetImageResultActivityに更新できません。クイズをQuizActivityで終了すると、ResultActivityはスコアのみを更新するだけで、画面に必要なテキストと画像は更新されません。UIが自動的にアップデートされていません。更新後にのみ変更されます。

ここにコードがありますが、私はsetTextに基づいて "スコア"に基づいていますが、変更を確認するためにUIを更新する必要があります。私は別のアクティビティからスコアを渡して、スコアに基づいてテキストを変更します。私に助けてください、前もって感謝します。

あなたとResultActivityを起動しようとします。コードから
//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"); 
     } 
    } 
} 
+0

あなたは言う: >私はResultActivityのsetTextとsetImageを更新できません。よく: 'setText'と' setImage'はメソッドではありません。私はTextViewとImageViewを意味すると思いますが、それはあなたが意味するものです。 –

+0

こんにちは@Ali Zeynaliは、ResultActivity.if(ハイスコア== 0){resulttext.settext( "xxx"); –

答えて

1

、:あなたはコードでの活動を更新する

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内のデータを更新する必要があります。

+0

ok thx。どのように私はResultActivityを開始する前にデータを更新することができますか?申し訳ありませんi am new:x –

+0

まずスコアを計算してください。計算を終えてSharedPreferencesに保存したら、アクティビティを開始することができます。 –

関連する問題