【问题标题】:Want to update Listview in Android without refershing the Activity想要在不刷新 Activity 的情况下更新 Android 中的 Listview
【发布时间】:2020-01-02 16:54:27
【问题描述】:

我使用 ListAdapter 在 Listview 中显示了一些数据。现在我希望列表视图在 x 秒后自动刷新。如何才能做到这一点? 我正在使用 AsyncTask 从服务器获取数据。

我试过这个:

public void reloadDeviceData() {
    handler.postDelayed(runnable = new Runnable() {
        public void run() {
            new GetDevices().execute();          // this method will contain your almost-finished HTTP calls
            handler.postDelayed(this, TIME);
        }
    }, TIME);
}

发生的事情是列表视图不刷新,而是在当前列表视图之后添加项目。 GetDevices 是这里扩展 AsyncTask 的类。

【问题讨论】:

  • 你能把GetDevices任务的代码贴出来吗?
  • 当你得到新数据时,你可以用你的adater设置notifydatasetchanged()
  • notifydatasetchanged() 是否实时检查数据?
  • 是的......
  • 使用 listview 不是 ggod 做法..请使用回收站视图

标签: android listview android-listview


【解决方案1】:

当您从 API 获得响应时。只需使用以下行 adapter.list.clear() 更新适配器列表,然后在 adapter.list.addAll(apiList) 之后,然后在您只需要通知列表视图适配器之后。

adapter.notifydatasetchanged();

【讨论】:

    【解决方案2】:

    我会推荐:

    1. 从性能角度使用 recyclerview。
    2. 检查新旧列表之间的差异差异并仅添加/更新这些项目。对于差异,您可以使用基本集合类方法或 DiffUittls lib

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2011-06-15
      相关资源
      最近更新 更多