Graham Borland
Graham Borland

Reputation: 60691

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 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?

Upvotes: 0

Views: 2157

Answers (2)

Jarek Potiuk
Jarek Potiuk

Reputation: 20097

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.

Upvotes: 1

Timo Ohr
Timo Ohr

Reputation: 7947

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.

Upvotes: 2

Related Questions