由于数据比较多,想用异步加载的方法。加载之前显示一个进度框,数据全部加载完毕之后再显示listview。
问题描述:
这个问题不经常出现,按返回键结束这个activity,然后再按主界面的按钮打开这个activity,就这样进进出出,四、五次的样子,就出错提示:The content of the adapter has changed but ListView did not receivea notification
问题程序:
Activity的 onCreate中
listview = (ListView) findViewById(R.id.listView1);
listview.addFooterView(view_footer);
lvAdapter = new ListViewAdapter(context);
listview.setAdapter(lvAdapter);
new ListTask(context).execute("");
异步类:
class ListTask extends AsyncTask<String,Integer,String>{ ProgressDialog pd;
Context context;
public ListTask(Context context){
this.context=context;
pd=new ProgressDialog(context);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(false);
pd.setCanceledOnTouchOutside(false);
}
@Override
protected String doInBackground(String... params) {
m_prepareList(context);
//读取数据库中的数据到lvAdapter的dataset中
return null;
}
@Override
protected void onPreExecute(){
pd.show();
}
@Override
protected void onPostExecute(String result){
lvAdapter.notifyDataSetChanged();
pd.dismiss();
}
}
解决方法:
最后试了好多方法都不能解决,看了网上的一篇文章:
http://blog.youkuaiyun.com/changemyself/article/details/8116670
说是数据更新后要及时notifyDataSetChanged();
后来想了一下,我的程序中的listview.setAdapter(lvAdapter);和lvAdapter.notifyDataSetChanged();毕竟隔了一个线程。所以试着把他们靠近点。
把listview.setAdapter(lvAdapter)放到onPostExecute中lvAdapter.notifyDataSetChanged()的上面,问题结解决。