Lukap
Lukap

Reputation: 31963

Different layout for the items in the listview

I have some extended cursor adapter, in witch I call super with the context and the resource layout for the item in the list, something like this.

call to super in my adapter:

super(activity, viewResource, c, false);

creation of my adapter:

new MyCursorAdapter(this, null, R.layout.my_list_item, null);

What I want to achieve is something like my stupid mock up made in paint. Put into words I want to have different kinds of layout for the items, for example I want all even items to have layout1 and all odd to have the layout2. So far I am able to give only one layout in this case R.layout.my_list_item. Is it possible to dynamically change the layout ? Is it possible to construct the adapter to have items with different layout ? My goal is to dynamically chose the layout of the item. I do not want to have just one layout for all of the items I want to have foe example two...

Thanks

enter image description here

Upvotes: 1

Views: 813

Answers (3)

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30825

Yes, you're going to have to do two things though. First, override the getItemViewType() method in your adapter so that you can be sure your bindView() only get's views that appropriate for a particular position in the list, like so:

public int getItemViewType(int position){
  if(correspondsToViewType1(position)){
    return VIEW_TYPE_1;
  }
  else(correspondsToViewType2(position)){
    return VIEW_TYPE_2;
  }
  //and so on and so forth.
}

Once you do that, just have a simple test in your bindView() that checks to see what type of view it should have recieved and setup things accordingly like so:

public void bindView(View view, Context context, Cursor cursor){
  if(correspondsToViewType1(cursor)){
    //Now we know view is of a particular type and we can do the 
    //setup for it
  }
  else if(correspondsToViewType2(cursor){
    //Now we know view is of a different type and we can do the 
    //setup for it
  }
}

Note that you're going to have to have to different methods for correpondsToViewType, one that takes a cursor and one that takes an int (for a position). The implementation for these will vary depending on what you want to do.

Note that doing things this way will allow you to reuse potentially recycled views. If you don't do this, your app is going to take a huge performance hit. Scrolling will be super choppy.

Upvotes: 2

Mark
Mark

Reputation: 7625

Just override the newView Method:

public class MyCursorAdapter extends CursorAdapter {

private final LayoutInflater inflater;
    private ContentType type;

public MyCursorAdapter (Context context, Cursor c) {
    super(context, c);
    inflater = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    if( cursor.getString(cursor.getColumnIndex("type")).equals("type1") ) {
                // get elements for type1
    } else {
                // get elements for type1
            }

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    if( cursor.getString(cursor.getColumnIndex("type")).equals("type1") ) {
        final View view = inflater.inflate(R.layout.item_type1, parent, false);
    } else {
        final View view = inflater.inflate(R.layout.item_type2, parent, false);
    }
    return view;
}

Upvotes: 0

Bobbake4
Bobbake4

Reputation: 24857

I'm guessing your extending SimpleCursorAdapter from the name of your custom adapter. You will want to override the function getView in your adapter and depending on the object in the list inflate a different layout and return that view.

EX:

     @Override
     public View getView (int position, View convertView, ViewGroup parent)
     {
            Object myObject = myList.get(position);

            if(convertView == null)
            {
                  if( something to determine layout )
                        convertView = inflater.inflate(Layout File);
                  else
                        convertView = inflater.inflate(Some Other Layout File);
            }

            //Set up the view here, such as setting textview text and such

            return convertView;
     }

This is just an example and is somewhat sudo code so it will need some adjustments for your specific situation.

Upvotes: 0

Related Questions