Shri Ksai
Shri Ksai

Reputation: 111

Simple Cursor Adapter sections

I am using simple cursor adapter to populate the listview. My data is coming from sqlyte database and i have two coumns. how do i add sections (headers) in listview.

i searched a lot but could'nt find proper sections examples using simple cursor adapter. Any help is greatly appreciated. here is my example.

startManagingCursor(cursor);

String[] from = {"Label","_id"};

int[] to = new int[]{R.id.Text1};

SimpleCursorAdapter cursorAdapter =
    new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);

listContent.setAdapter(cursorAdapter);

I need to add sections e.g

Section 1 Orange Banana Apple Sextion 2 red blue black Section 3 breakfast lunch

Upvotes: 1

Views: 755

Answers (1)

MinceMan
MinceMan

Reputation: 7592

This is actually quite difficult to do as you need to remap your cursor positions. There is a library that can help do this for you. The following is code that you can use to implement alphabetical sections with this library.

@Override 
protected Object getSectionFromCursor(Cursor cursor) {
    int columnIndex = cursor.getColumnIndex("Label");
    String name = cursor.getString(columnIndex);
    return name.toUpperCase().substring(0, 1); 
}

Also check out the sample app where they sort their employees by department.

https://github.com/twotoasters/SectionCursorAdapter

Upvotes: 1

Related Questions