Java ListActivity的Runnable中的无限循环
我对我的Java ListActivity的Runnable中的无限循环,java,android,runnable,Java,Android,Runnable,我对我的活动的可运行实现有一个奇怪的问题: 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 androi
活动的可运行实现有一个奇怪的问题:
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();
}
};
}
包bc.qz.client.android.activity;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.UUID;
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入bc.qz.client.android.adapter.ScoreListAdapter;
导入bc.qz.client.android.proxy.RemoteServletCaller;
导入de.bc.qz.business.Score;
公共类ScoreActivity扩展了ListActivity{
私人共享参考mSharedPreferences;
专用RemoteServletCaller mRemoteServletCaller;
私有可运行lviewscore可运行;
私有字符串mUuid;
私人弦乐博物馆名称;
private ProgressDialog mProgressDialog=null;
私有ScoreListAdapter mScoreListAdapter;
List mAllScore=newarraylist();
创建公共空间(捆绑冰柱){
超级冰柱;
mRemoteServletCaller=新的RemoteServletCaller();
mSharedPreferences=this.getSharedReferences(“de.bc.qz.client.SharedReferences”,
上下文。模式(私人);
mUuid=mSharedPreferences.getString(“uuid”,null);
mUsername=mSharedPreferences.getString(“用户”,null);
mScoreListAdapter=新的ScoreListAdapter(此为mAllScore);
setListAdapter(mScoreListAdapter);
lViewScoreRunnable=new Runnable(){
@凌驾
公开募捐{
loadAllScore(mUsername,mUuid);
}
};
线程线程=新线程(null,lviewscorerunable,“MagentoBackground”);
thread.start();
mProgressDialog=ProgressDialog.show(ScoreActivity.this,“请稍候…”,
“正在检索数据…”,true);
}
私有void loadAllScore(字符串pUsername、字符串pUuid){
试一试{
if(null==mUuid | | mUsername==null){
mUuid=UUID.randomUUID().toString();
mSharedPreferences.edit().putString(“uuid”,mUuid).commit();
addAll(mRemoteServletCaller.getAllScore(1,10));
}否则{
addAll(mRemoteServletCaller.getAllScore(mUsername,mUuid));
}
}捕获(例外e){
返回;
}
runOnUiThread(returnRes);
}
private Runnable returnRes=new Runnable(){
@凌驾
公开募捐{
if(mAllScore!=null&&mAllScore.size()>0){
mScoreListAdapter.notifyDataSetChanged();
对于(int i=0;i
当我使用调试器运行方法时,mAllScore
的大小为10。
由于某种原因,For循环以一个无止境的循环结束
我不知道为什么?
请帮我换衣服
if (mAllScore != null && mAllScore.size() > 0) {
mScoreListAdapter.notifyDataSetChanged();
for (int i = 0; i < mAllScore.size(); i++)
mScoreListAdapter.add(mAllScore.get(i));
}
你怎么称呼这个可运行的?什么是mScoreListAdapter.add()
?可能mScoreListAdapter.add()
正在将项目添加到mAllScore
中,因此i
永远不会达到size()
,但这只是在没有看到相关代码时的猜测。因为mScoreListAdapter.notifyDataSetChanged()也存在于“if条件”之外你可以从这里移除它。
if (mAllScore != null && mAllScore.size() > 0) {
for(int i = 0,N=mAllScore.size();i<N;i++) {
mScoreListAdapter.add(mAllScore.get(i));
}
mScoreListAdapter.notifyDataSetChanged();
}
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);
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.