开发者

Android adapter with section headings: performance problems

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 h开发者_JAVA技巧eadings) 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜