notifyDataSetChanged()在我的适配器没有更新的列表视图,为什么呢? [英] notifyDataSetChanged() on my adapter does not update the listview, why?

查看:492
本文介绍了notifyDataSetChanged()在我的适配器没有更新的列表视图,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动的延伸listactivity,扩大在这一类我有一个扩展baseadapter一类。

I have a activity that extends listactivity, extended in this class i have a class that extends baseadapter.

现在在我的listactivity我有这样的onCreate

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();
}

这工作,因为它是应该的,但我的问题是onPostExecute我更新的数据结构的列表适配器使用。 为什么不能我只是叫notifyDataSetChanged ??

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 ??

如果我有该行的观点不更新自己,但如果我用的是线下,我做setListAdapter,这一切工作。

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.

推荐答案

如果该适配器已经设置,重新设置它不会刷新列表视图。相反,第一次检查,如果列表视图有一个适配器,然后调用相应的方法。

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.

BuildingAdapter adapter = new BuildingAdapter(context);

    if(getListView().getAdapter() == null){ //Adapter not set yet.
     setListAdapter(adapter);
    }
    else{ //Already has an adapter
    adapter.notifyDataSetChanged();
    }

这篇关于notifyDataSetChanged()在我的适配器没有更新的列表视图,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆