views:

24

answers:

1

I have a customized Cursor based adapter for my ListView. Each Cursor has a few columns of data, sorted by the priority column.

I know you can create a disabled list item to act as a sort of header, like in the Market app (the little green labels).

What I would like to do is display a header for each group of items with a different priority. They are already sorted by priority.

Example data:

title, priority
note1, high
note3, high
note2, low
note4, low

Example of what I want in ListView:

===High Priority====
-note1
-note3
===Low Priority=====
-note2
-note4

Any ideas on how I could do this?

A: 

Having heading rows in a ListView is a matter of custom-crafting an appropriate ListAdapter. Have areAllItemsEnabled() return false and implement isItemEnabled() as needed.

Where things get tricky is in your chosen data model. Your ListAdapter will need to return 6 from getCount(), since you want six rows in your list. Your underlying data has four rows. That means you are going to have to write a fair bit of code to translate positions from what the ListView thinks (6) to what your data thinks (4), and handling the header rows for the other two positions.

CommonsWare
Is there a data model I could use that would work better? Maybe build a second data model between the cursor and the adapter?
CodeFusionMobile
@CodeFusionMobile: The only way I have done headings so far is where I have individual Cursors -- in your case, one Cursor for high and one Cursor for low. See this sample project: http://github.com/commonsguy/cw-advandroid/tree/master/ListView/Sections/ If your number of priorities is fairly low, you could use the same technique, but if you are seeking a lot of headings, it is probably more efficient to cook up an adapter that can work with your existing data model.
CommonsWare