我的RunnableActivity实现存在一个奇怪的问题:

package bc.qz.client.android.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import bc.qz.client.android.adapter.ScoreListAdapter;
import bc.qz.client.android.proxy.RemoteServletCaller;
import de.bc.qz.business.Score;

public class ScoreActivity extends ListActivity {

    private SharedPreferences mSharedPreferences;
    private RemoteServletCaller mRemoteServletCaller;
    private Runnable lViewScoreRunnable;
    private String mUuid;
    private String mUsername;
    private ProgressDialog mProgressDialog = null;
    private ScoreListAdapter mScoreListAdapter;
    List<Score> mAllScore = new ArrayList<Score>();

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        mRemoteServletCaller = new RemoteServletCaller();

        mSharedPreferences = this.getSharedPreferences("de.bc.qz.client.sharedpreferences",
                Context.MODE_PRIVATE);
        mUuid = mSharedPreferences.getString("uuid", null);
        mUsername = mSharedPreferences.getString("user", null);

        mScoreListAdapter = new ScoreListAdapter(this, mAllScore);
        setListAdapter(mScoreListAdapter);

        lViewScoreRunnable = new Runnable() {
            @Override
            public void run() {
                loadAllScore(mUsername, mUuid);
            }
        };
        Thread thread = new Thread(null, lViewScoreRunnable, "MagentoBackground");
        thread.start();
        mProgressDialog = ProgressDialog.show(ScoreActivity.this, "Please wait...",
                "Retrieving data ...", true);
    }

    private void loadAllScore(String pUsername, String pUuid) {
        try {
            if (null == mUuid || mUsername == null){
                mUuid = UUID.randomUUID().toString();
                mSharedPreferences.edit().putString("uuid", mUuid).commit();
                mAllScore.addAll(mRemoteServletCaller.getAllScore(1, 10));
            }else{
                mAllScore.addAll(mRemoteServletCaller.getAllScore(mUsername, mUuid));
            }
        } catch (Exception e) {
            return;
        }
        runOnUiThread(returnRes);
    }

    private Runnable returnRes = new Runnable() {

        @Override
        public void run() {
            if (mAllScore != null && mAllScore.size() > 0) {
                mScoreListAdapter.notifyDataSetChanged();
                for (int i = 0; i < mAllScore.size(); i++)
                    mScoreListAdapter.add(mAllScore.get(i));
            }
            mProgressDialog.dismiss();
            mScoreListAdapter.notifyDataSetChanged();
        }
    };
}


当我使用调试器进入run方法时,mAllScore的大小为10。
由于某种原因,for循环以无限循环结束。

我不知道为什么?
请帮我...

最佳答案

更改

   if (mAllScore != null && mAllScore.size() > 0) {
        mScoreListAdapter.notifyDataSetChanged();
        for (int i = 0; i < mAllScore.size(); i++)
            mScoreListAdapter.add(mAllScore.get(i));
    }




  if (mAllScore != null && mAllScore.size() > 0) {
      for(int i = 0,N=mAllScore.size();i<N;i++) {
          mScoreListAdapter.add(mAllScore.get(i));
      }
      mScoreListAdapter.notifyDataSetChanged();
  }


确保您正在UI线程中调用notifyDataSetChanged()。

并且我认为您在创建Adapter之类的对象时将mAllScore传递给Adapter类

 mAllScore = new ArrayList<String>();
 // you are adding items to this list
 // and creating adapter like
 mScroreListAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mAllScore);


因此,当您使用adapter.add(Object)将项目添加到适配器时,它也会将该对象也添加到mAllScore列表中。 (意味着mAllScore列表生效)

所以在for循环中

 for(int i = 0;i<mAllScore.size();i++) {
     mScrollAdapter.add(mAllScore.get(i)); // indirectly adding items to mAllScore
     // and thus the condition i<mAllScore.size() will never be false.

09-04 22:01