我有一个扩展listactivity的活动,在这个类中扩展我有一个扩展baseadapter的类。
现在在我的列表活动中我有这个 onCreate
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new BuildingAdapter(this));
final ProgressDialog loading = new ProgressDialog(this);
loading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
loading.setTitle("Laddar");
loading.setMessage("Hämtar data från servern");
loading.show();
new AsyncTask<Void, Void, DataPacket.Data>()
{
@Override
protected DataPacket.Data doInBackground(Void... params){
return ServerConnection.getBuildings();
}
@Override
protected void onPostExecute(DataPacket.Data param) {
((MainenanceApplication)getApplication()).setDataStructure(param);
loading.dismiss();
//((BuildingAdapter)getListAdapter()).notifyDataSetChanged();
setListAdapter(new BuildingAdapter(BuildingSelectionActivity.this));
}
}.execute();
}
这按预期工作,但我的问题是在 onPostExecute 中我更新了列表适配器使用的数据结构。
为什么我不能直接调用notifyDataSetChanged?
如果我有该行,视图不会自行更新,但如果我使用 setListAdapter 下的行,则一切正常。
如果适配器已经设置,再次设置将不会刷新列表视图。相反,首先检查列表视图是否有适配器,然后调用适当的方法。
我认为在设置列表视图时创建适配器的新实例不是一个好主意。相反,创建一个对象。
BuildingAdapter adapter = new BuildingAdapter(context);
if(getListView().getAdapter() == null){ //Adapter not set yet.
setListAdapter(adapter);
}
else{ //Already has an adapter
adapter.notifyDataSetChanged();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)