1

我对变量 scoretemp 有疑问。它总是返回 0。在 if 条件下它正在工作。

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

1 回答 1

1

回调方法 requestControllerDidReceiveResponse 是异步调用的,所以它可以并且很可能会在 TV2.setText(""+scoretemp); 之后调用。叫做。

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

于 2012-11-29T12:45:13.437 回答