尝试从asynctask更新我的片段中的listview时出现此错误:
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是类扩展Application,它汇总了我需要的变量和方法
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);
}
如何解决?
答案 0 :(得分:2)
您不应该在doInBackground()中调用addToLog()(或任何其他UI操作)。 要么收集doInBackground中的所有内容,将其作为结果返回,然后更新onPostExecute中的列表,要么使用publishProgress()并更改onProgressUpdate()中的列表内容...
onPostExecute和onProgressUpdate在UI线程中执行,doInBackground在不同的线程中执行。
答案 1 :(得分:1)
Ui更新是从asynctask中的onpostexecute()方法完成的。这样做,它会正常工作,因为onintexecute在doibackground()完成其工作后在ui线程上运行。
答案 2 :(得分:0)
您应该使用以下内容在主线程中运行更新:
MainActivity.this.runOnUiThread(new runnable(){
public void run(){
Log.d("UI thread", "I am the UI thread");
}
});
你也可以查看这篇文章: Android basics: running code in the UI thread
答案 3 :(得分:0)
尝试使用
MainActivity.this.runOnUiThread(new runnable()
{
public void run()
{
LogFragment.mLogAdapter.notifyDataSetChanged();
LogFragment.mLogAdapter.setSelectToLast();
StatFragment.tvWater.setText(String.valueOf(AppStat.WATER));
}
}
您的onProgressUpdate
方法中的
答案 4 :(得分:0)
尝试在onPostExecute上调用适配器上的notifyDataSetChanged()。