views:

50

answers:

3

Hi, Is there a way to make a list with different views? I mean that row in posiotion X will have layout "X.xml" and row number Z will have layout "Y.xml"?

I've tried to manipulate ListRowAdapter() and getView() in a few ways but with no success...

A: 

I don't think there is an easy way to do it out of the box. Depending on how different your layouts are, you might be able to come up with a single layout, and then show or hide elements of it in getView depending on the row. If you set an item to Layout.GONE, its as if it isn't there.

Mayra
that is what I currently have but I need to change that - one layout is just one TextView and the other is 6..
gilmad
+5  A: 

Hi, Is there a way to make a list with different views? I mean that row in posiotion X will have layout "X.xml" and row number Z will have layout "Y.xml"?

Sure.

Step #1: Create an Adapter class, by extending BaseAdapter, ArrayAdapter, CursorAdapter, etc.

Step #2: Implement getViewTypeCount() to return how many different row types there are

Step #3: Implement getItemViewType() to return a number between 0 and the value returned by getViewTypeCount(), indicating which row type a given position will use

Step #4: Override getView() (or newView() and bindView() for CursorAdapter) and have it create the right row

CommonsWare
thanks for the quick response!That is really helpful.Is there any example that you know of? I learn faster from examples... :)
gilmad
Well, you can look at: http://github.com/commonsguy/cw-advandroid/tree/master/ListView/Sections/ or http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/ -- the second one is a bit old
CommonsWare
A: 

I solved it in another way: in the ListRowAdapter -> getView() I've made an if() statement on the inflater:

LayoutInflater inflater=context.getLayoutInflater();
View row=inflater.inflate(R.layout.X, null);
if(somthing)
{
row=inflater.inflate(R.layout.Y, null);
//whatever
}
and in the end: return row;

works like a charm :)

gilmad