尝试从异步任务更新我的片段中的列表视图时出现此类错误:
The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread.
我的 AsyncTask 类
protected Void doInBackground(Void... params) {
int nextStep;
BaseAnt tempAnt = null;
ArrayList<BaseAnt> antList = new ArrayList<BaseAnt>();
while (true) {
Log.d("myLogs", "Start from APP");
nextStep = RandomNumber.getRandomNumber(3);
switch (nextStep) {
case 0:
tempAnt = new GuardAnt();
AppStat.addToLog("Guard");
break;
case 1:
tempAnt = new MotherAnt();
AppStat.addToLog("Mother born");
break;
case 2:
tempAnt = new WorkerAnt();
AppStat.addToLog("Worker born");
break;
default:
break;
}
antList.add(tempAnt);
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i < antList.size(); i++)
antList.get(i).eat();
publishProgress(String.valueOf(AppStat.WATER));
if (AppStat.WATER < 0)
break;
}
return null;
}
protected void onPostExecute(String result) {
}
@Override
protected void onProgressUpdate(String... values) {
Log.d("myLogs", "onProgressUpdate");
LogFragment.mLogAdapter.notifyDataSetChanged();
LogFragment.mLogAdapter.setSelectToLast();
StatFragment.tvWater.setText(String.valueOf(AppStat.WATER));
super.onProgressUpdate(values);
}
AppStat 是类扩展应用程序,包含有关我需要的变量和方法的信息
public static void addToLog(String str) {
if (listLog.size() > 256)
listLog.remove(0);
listLog.add(getNowtime() + ": " + str);
}
在 LogAdapter 扩展 BaseAdapter:
public void setSelectToLast() {
Log.d("myLogs","UpdateToLast");
notifyDataSetChanged();
LogFragment.lvList.setSelection(getCount() + 1);
}
如何解决?