我的适配器上的notifyDataSetChanged() 不更新列表视图,为什么?
我有一个扩展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 下的行,则一切正常。
I have a activity that extends listactivity, extended in this class i have a class that extends baseadapter.
now in my listactivity i have this 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();
}
This works as it's supposed to, but my question is in onPostExecute I update the datastructure that the list adapter uses.
Why cant I just call notifyDataSetChanged ??
If I have that line the view does not update itself, but if I use the line under where I do setListAdapter, it all works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果适配器已经设置,再次设置将不会刷新列表视图。相反,首先检查列表视图是否有适配器,然后调用适当的方法。
我认为在设置列表视图时创建适配器的新实例不是一个好主意。相反,创建一个对象。
If the adapter is already set, setting it again will not refresh the listview. Instead first check if the listview has a adapter and then call the appropriate method.
I think its not a very good idea to create a new instance of the adapter while setting the list view. Instead, create an object.
您是否尝试过使用 AsyncTaskLoader 而不是 AsyncTask 来实现此目的。 Loaders 正是为这种东西而设计的。请注意,即使 Loaders 在 API-10 之前才可用,您仍然可以通过 android 从 API-4 及更高版本支持 Pacakge。
Have you tried using an AsyncTaskLoader instead of an AsyncTask for this. It's this kind of stuff that Loaders were exactly designed for. Note that even though Loaders weren't available until API-10 you can still easily access them via the android Support Pacakge from API-4 and up.
唯一可以更新 UI 的位置是
onProgressUpdate(...);
。从您的doInBackground(...)
中,调用publishProgress(...)
。The only place you can update the UI is in
onProgressUpdate(...);
. From yourdoInBackground(...)
, callpublishProgress(...)
.