变量不工作的android

时间:2012-11-29 12:22:56

标签: java android local-variables

我有变量scoretemp的问题。它总是返回0。 在内部,如果条件它正在工作。

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);

1 个答案:

答案 0 :(得分:1)

回调方法requestControllerDidReceiveResponse是异步调用的,所以它可以并且可能会在TV2.setText(“”+ scoretemp)之后调用;被称为。

您可以在requestControllerDidReceiveResponse方法中进行更新(在GUI线程上调用),也可以在更改值时触发某种propertychangedevent,然后由GUI线程处理。