对于从后台线程定期更新并跨多个活动进行维护的 ListView,什么是好方法?

发布于 2024-10-14 04:47:08 字数 876 浏览 4 评论 0原文

首先是我正在寻找的一些背景信息和参数。

  • 该应用程序有多个需要访问单个列表的活动。我正在使用 Application 类,以便在所有应用程序中维护此列表的活动。
  • 该列表是自定义对象的列表。
  • 有一个活动有一个 ListView 来显示此列表中的项目。
  • 还有其他活动需要能够访问此列表,但不显示 ListView。
  • 该列表可以被排序或过滤。
  • 该列表可以随时从后台线程更新(添加、删除、更新内容),并且这些更改应在查看列表时立即反映出来。

到目前为止,我的方法是通过应用程序中的方法管理列表,包括管理列表的任何更新。如果用户当前处于包含 ListView 的 Activity 中,则它正在侦听列表的更新。当它听到更新时,它会从应用程序复制列表并将其发送到 ListViews 自定义 ArrayAdapter。 ArrayAdapter 获取此列表副本并替换基本列表,对其调用当前排序和过滤器,然后替换列表中实际显示的列表并调用 notifyDataSetChanged()。这都是为了避免在列表从 UI 线程之外发生更改或未调用 notifyDataSetChanged() 的情况下适配器出现任何问题。

这一切都工作得很好,问题是使用这种方法时,同一列表有多个副本,这会降低该列表在其他地方的可用性并产生不必要的内存使用。如果在一定时间间隔内出现大量更新,它还会创建大量复制。

所以我想看看其他人是否有任何方法可以允许 ListView 直接引用主列表源,但仍然避免从 UI 线程更新的问题?

谢谢

First some background info and parameters of what I am looking for.

  • The application has several activities that need access to a single list. I am using the Application class in order to maintain this list across all of the activities.
  • The list is a list of a custom object.
  • There is one activity that has a ListView to show the items in this list.
  • There are other activities that need to be able to access this list, but do not show a ListView.
  • The list may be sorted or filtered.
  • The list can be updated (add, remove, update content) from a background thread at any time and those changes should be reflected immediately when viewing the list.

So far my approach is to have the list managed via methods in the Application, including managing any updates to the list. If the user is currently in the Activity with the ListView in it, it is listening for updates to the list. When it hears an update it copies the list from the Application and sends it to the ListViews custom ArrayAdapter. The ArrayAdapter takes this list copy and replaces the base list, calls the current sort and filter on it and then replaces the list that is actually displayed in the list and calls notifyDataSetChanged(). This is all to avoid any issues the Adapter will have if the list changes from off the UI Thread or without having notifyDataSetChanged() called.

This all works just fine, the problem is that with this approach there are multiple copies of the same list which decreases the usability of this list in other places and creates unneeded memory use. It also can create a lot of copying if a lot of updates come in a certain interval.

So I am looking to see if anyone else has any approaches that would allow the ListView to reference the main list source directly but still avoid issues with updating from off the UI thread?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2024-10-21 04:47:08

我不知道我的方法是否更好。我创建一个适配器,其中包含项目的链接列表。我保留对该适配器的静态引用,并且当需要更新时,其他活动等。更新列表并调用adapter.notifyDataSetChanged()

我认为这仅使用数据的一份副本。

I don't know that my approach is any better. I create an Adapter, which holds a LinkedList of items. I keep a static reference to that Adapter, and when updates are needed, the other Activities,etc. update the List and call adapter.notifyDataSetChanged()

I think this only uses one copy of the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文