2012-11-29 60 views
1

我遇到變量scoretemp問題。它總是返回0. 裏面,如果條件它的工作。變量不工作android

for (final Score score2 : scores) { 
    scoretemp = score2.getResult(); 
} 

但是當我嘗試在最後一行中得到結果。

TV2.setText(""+scoretemp); 

它沒有任何理由總是爲零。

代碼:

BScore.setOnClickListener(new View.OnClickListener() { 

    double scoretemp; 

    public void onClick(View arg0) { 
    BScore.setVisibility(View.GONE); 
    sp.play(click, 1, 1, 0 , 0, 1); 
    int scoreresult = totalscore; 
    final RequestControllerObserver observer2 = new RequestControllerObserver() { 
     public void requestControllerDidReceiveResponse(final RequestController requestController) { 
     // get the scores from our controller 
     final ScoresController scoresController = (ScoresController) requestController; 
     final List<Score> scores = scoresController.getScores(); 
     if(scores!=null){ 
      for (final Score score2 : scores) { 
      scoretemp = score2.getResult(); 
      } 
      // we're done! 
      dismissDialog(DIALOG_PROGRESS); 
     } 
     } 
    public void requestControllerDidFail(final RequestController aRequestController, final Exception anException) { 
     dismissDialog(DIALOG_PROGRESS); 
     Toast.makeText(getApplicationContext(), "لا يوجد إتصال بالأنترنت أو خطأ في إستقبال البيانات", Toast.LENGTH_LONG).show(); 
} 
    }; 
// set up a ScoresController with our observer 
ScoresController scoresController2 = new ScoresController(observer2); 
showDialog(DIALOG_PROGRESS); 
scoresController2.setMode(mode); 
scoresController2.loadRangeForUser(Session.getCurrentSession().getUser()); 
TV2.setText(""+scoretemp); 
+3

變量不起作用,它們會有所不同! – Juvanis

+1

回調方法requestControllerDidReceiveResponse被稱爲異步,所以它可以也可能會在TV2.setText(「」+ scoretemp)之後被調用。叫做。 –

+1

@RobeEleckers我認爲這是問題在這裏。最後一行應該在偵聽器內部,而不在其外部。 –

回答

1

回調方法requestControllerDidReceiveResponse異步調用,因此它可以與很可能會在TV2.setText( 「」 + scoretemp)之後被調用;叫做。

您可以在requestControllerDidReceiveResponse方法內執行更新(在GUI線程上調用),也可以在值更改後觸發某種propertychangedevent,然後由GUI線程處理。