【发布时间】:2020-03-12 15:44:38
【问题描述】:
notifyDataSetChanged 有问题。我有一个从 sqlite 数据库中检索数据的列表。问题是我知道每次使用 list 类的 add 方法时都必须调用 notifyDataSetChanged 。我不明白为什么我的列表在没有 notifyDataSetChange() 的情况下调用 addAll() 后显示数据。我也尝试过使用 add() 但结果是一样的。我需要答案,因为我想很好地理解 notifyDataSetChange() 是如何工作的。 片段代码:
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()?
【问题讨论】:
-
public static List<Wherehouse> mListFoodsIn;与此相关的一切都必然是错误的和/或导致以后崩溃。包括多线程问题。 -
我可以再解释一下吗?我是初学者
标签: java android listview arraylist notifydatasetchanged