Search code examples
androidcursoradapter

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?


Solution

  • 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.