带有章节标题的 Android 适配器:性能问题
我有一个自定义适配器来显示带有部分标题的项目列表。我看过 Jeff Sharkey 的 SeparatedListAdapter 和 CommonsWare 的 MergeAdapter 作为如何实现此目标的示例,我现在有了一个解决方案它的工作原理是为每个部分的内容提供一个单独的适配器。
但这会造成很大的性能问题。就我而言,列表中可能有数千个项目,每个项目都有一个关联的日期,我想使用该日期作为具有该日期的所有项目的部分标题。
因此,如果没有章节标题,我将有一个游标,它返回按日期排序的项目。又好又容易。
对于章节标题,我目前正在执行以下操作:
- 使用一个游标来选择数据集中的所有不同日期
- 对于每个不同日期,使用一个单独的游标来返回与该日期匹配的项目
- 倒入日期(章节标题)并将每个日期的项目的 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:
- One Cursor to select all distinct dates in the dataset
- For each distinct date, a separate Cursor to return the items matching that date
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想最简单的方法是在每个 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.您可以尝试 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.