没有 notifyDataSetChange 的列表视图刷新 [英] Listview refresh without notifyDataSetChange

查看:22
本文介绍了没有 notifyDataSetChange 的列表视图刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

notifyDataSetChanged 有问题.我有一个从 sqlite 数据库中检索数据的列表.问题是我知道每次使用列表类的 add 方法时都必须调用 notifyDataSetChanged .我不明白为什么我的列表在没有 notifyDataSetChange() 的情况下调用 addAll() 后显示数据.我也尝试使用 add() 但结果是一样的.我需要答案,因为我想很好地了解 notifyDataSetChange() 是如何工作的.片段代码:

There is a problem with notifyDataSetChanged. I have a list which retrieves data from sqlite database. The problem is that I know that I have to call notifyDataSetChanged everytime I use add method of list class. I can't understand why my list shows data after addAll() calling without the notifyDataSetChange(). I also tried using add() but result is the same. I need answer because I want to understand very well how notifyDataSetChange() works. Fragment code:

 public static List<Wherehouse> mListFoodsIn;  
wherehouseAdapter wherehouseAdapter;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_wherehouse, container, false);

    wherehouseList = v.findViewById(R.id.wherehouseList); 
    final DBManager db = new DBManager(getActivity());

    mListFoodsIn = new ArrayList<>();
    wherehouseAdapter = new wherehouseAdapter(getActivity(), mListFoodsIn);
    new GetWherehoouseAsync(getActivity(),mListFoodsIn, wherehouseList, wherehouseAdapter).execute();  
    wherehouseList.setAdapter(wherehouseAdapter);

异步类:

public static class GetWherehoouseAsync extends AsyncTask<Void, Void, Void>{

    Context mContext;
    wherehouseAdapter mAdapter;
    DBManager db;
    List<Wherehouse> mList;

    ListView listViewWherehouse;

    public GetWherehoouseAsync(Context mContext, List<Wherehouse> list, ListView lv, wherehouseAdapter adapter) {
        this.mContext = mContext;
        db = new DBManager(mContext);
        this.mList = list;
        this.listViewWherehouse = lv;
        mAdapter = adapter;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }

    @Override
    protected Void doInBackground(Void... voids) {

      //  List<Wherehouse> tmpList = db.GetWherehouse();


        mList.addAll(db.GetWherehouse());


        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

      //  mAdapter.notifyDataSetChanged();
    }

这可能是很自然的场景,因为我在 onCreateView 中调用了 async()?

It maybe natural scenario because I call async() in onCreateView?

推荐答案

您在更新列表 mListFoodsIn 后将适配器应用到列表视图,因为您的数据来自数据库而不是通过网络调用结果返回比较快.因此,正确的列表会在创建时正确应用于列表视图.如果数据在片段的生命周期内发生变化,则需要通知适配器.

You're applying the adapter to your listview after updating the list mListFoodsIn, since your data is coming from a Database not via a network call the result returns relatively fast. So the correct list is applied to the listview on creation correctly. If the data were to change during the lifecycle of your fragment, then you would need to notify the adapter.

notifyDataSetChanged()

通知附加的观察者底层数据已经被改变了

Notifies the attached observers that the underlying data has been changed

这篇关于没有 notifyDataSetChange 的列表视图刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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