带有章节标题的 Android 适配器:性能问题

发布于 2024-10-03 04:11:23 字数 821 浏览 1 评论 0原文

我有一个自定义适配器来显示带有部分标题的项目列表。我看过 Jeff Sharkey 的 SeparatedListAdapter 和 CommonsWare 的 MergeAdapter 作为如何实现此目标的示例,我现在有了一个解决方案它的工作原理是为每个部分的内容提供一个单独的适配器。

但这会造成很大的性能问题。就我而言,列表中可能有数千个项目,每个项目都有一个关联的日期,我想使用该日期作为具有该日期的所有项目的部分标题。

因此,如果没有章节标题,我将有一个游标,它返回按日期排序的项目。又好又容易。

对于章节标题,我目前正在执行以下操作:

  1. 使用一个游标来选择数据集中的所有不同日期
  2. 对于每个不同日期,使用一个单独的游标来返回与该日期匹配的项目
  3. 倒入日期(章节标题)并将每个日期的项目的 SimpleCursorAdapters 单独放入我的自定义适配器中。

这需要生成比我想要的更多的数据库查询和游标,并且在 ListView 出现之前有几秒钟的延迟。

我怀疑可能有一种更简单的解决方案,其中 getView 做了一些聪明的事情并检测连续项目之间的日期何时发生变化,然后自行潜入一个新标题,因此只需要一个游标。谁能建议一种方法来做到这一点?

I have a custom adapter to display a list of items with section headings. I've looked at Jeff Sharkey's SeparatedListAdapter and CommonsWare's MergeAdapter as examples of how to achieve this, and I now have a solution which works by providing a separate adapter for the content of each section.

This creates a big performance problem, though. In my case, there are potentially thousands of items in the list, each with an associated date, and I want to use the date as section heading for all the items with that date.

So, without section headings, I'd have a single Cursor which returns the items sorted by date. Nice and easy.

With section headings, I'm currently doing this:

  1. One Cursor to select all distinct dates in the dataset
  2. For each distinct date, a separate Cursor to return the items matching that date
  3. Pour the dates (section headings) and separate SimpleCursorAdapters for each date's items, into my custom adapter.

This requires many more database queries and Cursors to be generated than I want, and there's a delay of several seconds before the ListView appears.

I suspect there might be a simpler solution where getView does something clever and detects when the date has changed between successive items, and then sneaks in a new heading by itself, thus requiring just the one Cursor. Can anyone suggest a way of doing this?

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

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

发布评论

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

评论(2

如梦 2024-10-10 04:11:23

我想最简单的方法是在每个 getView 调用中检查前一个项目是否具有不同的日期,如果是,则只需将标题嵌入当前视图中。

I guess the easiest way would be to check within every getView call whether the previous item has a different date, and if so to simply embed the header within the current view.

一袭水袖舞倾城 2024-10-10 04:11:23

您可以尝试 http://code.google.com/p/android-section-list /,只需要后面有一个光标(返回光标+一个对象中的部分)。然而,无论如何它都必须遍历所有元素(一次)来计算结果列表+标题的大小(列表适配器需要) - 所以它可能仍然很慢。

You can try http://code.google.com/p/android-section-list/, which requires just single cursor behind (returning cursor + section in one object). However it will anyhow have to go through all the elements (once) to calculate the size of resulting list+headers (needed by list adapter) - so it might still be slow.

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