ListView刷新,而无需notifyDataSetChange [英] Listview refresh without notifyDataSetChange

查看:335
本文介绍了ListView刷新,而无需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

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

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