Search code examples
androidsqliteadaptersimplecursoradapter

Android - Does SimpleCursorAdapter allow multiple layouts like BaseAdapter?


I know you can create a custom Adapter extending BaseAdapter and create various layouts which can be inflated depending on which row the AdapterView is at..

But is there any way to get a simple amount of customization with a SimpleCursorAdapter?

Eg. I have a database and I would like to query it and return the results to a ListView with alternating row layouts.

Will SimpleCursorAdapter do? Or are there any elegant solutions for this?

Cheers


Solution

  • But is there any way to get a simple amount of customization with a SimpleCursorAdapter?

    Just like BaseAdapter, you can extend CursorAdapter or SimpleCursorAdapter to do your customization.

    Eg. I have a database and I would like to query it and return the results to a ListView with alternating row layouts.

    If you are only alternating a minor thing like row color, you can simply override bindView() and check if(cursor.getPosition() % 2 == 0) (or == 1) to set the appropriate background color.

    If you are using different types of or numbers of Views in each layout, you need to override getViewTypeCount() and getItemViewType(). Then use getItemViewType() in newView() to load the appropriate layout and in bindView() to display the appropriate data.