32

How do you make those standard header labels in the ListView Control?

An example would be in the contacts application you see the first letter of the grouped contacts for each letter in the alphabet.

If possible please provide some code snippets on how to construct the grouped data and the display of the ListView.

Jeremy Edwards
  • 14,620
  • 17
  • 74
  • 99

1 Answers1

32

You can use my MergeAdapter or Jeff Sharkey's SeparatedListAdapter for that.

In the case of MergeAdapter, you would add an ordinary View (e.g., TextView) for a section header, then an Adapter for the contents of that section. Lather, rinse, repeat.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    Is this the standard methodology that google used in their applications? I saw that there were some custom implementations but it seemed like this type of work would have been integrated into the API at this point. Most custom implementations I saw were for Android 0.9. – Jeremy Edwards Dec 28 '09 at 03:22
  • 1
    "Is this the standard methodology that google used in their applications?" I have no idea. You would have to look at their source code on http://source.android.com. "but it seemed like this type of work would have been integrated into the API at this point" Outside of the preference system, there is nothing built into Android that provides an API for having section headings. Jeff Sharkey's implementation was originally for 0.9 but probably still works; my two implementations work up through 2.0.1. – CommonsWare Dec 28 '09 at 13:10
  • Link for SectionedAdapter, seems broken. All that is in ListView folder is HeaderFooter, RateListView and Selector. – Diederik Jun 12 '12 at 07:20
  • 1
    What about `ExpandableListView` (http://developer.android.com/reference/android/widget/ExpandableListView.html)? It has similar functionality. – Muxa May 05 '14 at 01:47