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);
變量不起作用,它們會有所不同! – Juvanis
回調方法requestControllerDidReceiveResponse被稱爲異步,所以它可以也可能會在TV2.setText(「」+ scoretemp)之後被調用。叫做。 –
@RobeEleckers我認爲這是問題在這裏。最後一行應該在偵聽器內部,而不在其外部。 –