【问题标题】:Call notifyDataSetChanged after each item I receive in AsyncTask在我在 AsyncTask 中收到的每个项目之后调用 notifyDataSetChanged
【发布时间】:2016-03-23 11:06:37
【问题描述】:

我使用AsyncTaskdoInBackground() 我有一个循环,我在其中进行大量网络调用。每次调用都会给我一个新的数据对象,我想将它添加到我的listView

我知道我可以在onPostExecute() 上致电notifyDataSetChanged() 并用我的所有物品更新我的清单,但我想知道当我收到新物品并更新我的listView 时是否有可能以某种方式致电notifyDataSetChanged()同一时刻,不必等到doInBackground 上的所有工作完成。

如果我无法使用 AsyncTask 执行此操作,那么处理此类事情的正确方法是什么?

谢谢

【问题讨论】:

标签: android listview android-asynctask


【解决方案1】:

doInBackground(...) 调用publishProgress(...) 并在您的AsyncTask 中覆盖onProgressUpdate(...) 并在那里调用notifyDataSetChanged()

doInBackground(...) 调用publishProgress(...) 指示AsyncTask 在主UI 线程上调用它的onProgressUpdate(...),因此您可以从那里更新ListView

【讨论】:

  • 请注意,因为每次插入时都会通知适配器,因此可以调用 notifyDataSetChanged() 而不是 notifyItemInsterted()。
  • @RusheelJain +1 但我相信notifyItemInsterted(...) 仅适用于RecyclerView 而@Mes 正在使用ListView。如果我错了,请纠正我。
  • @camelCaseCoder 我认为你是对的,因为我找不到notifyItemInsterted();
  • @camelCaseCoder 我认为你是对的。这是recyclerView的特性。
猜你喜欢
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 2018-11-29
相关资源
最近更新 更多