8

I want to create Listview in which I want different layout for all different row. Then how can I create custom adapter for set different layout for different row.

Any help would be greatly appreciated.

Thank you in Advance.

6
  • check this stackoverflow.com/questions/4777272/… Commented Nov 1, 2013 at 8:34
  • use a custom listview with a custom adapter Commented Nov 1, 2013 at 8:34
  • can you provide medemo code for this Commented Nov 1, 2013 at 8:35
  • @ArvindKanjariya stackoverflow.com/questions/18868194/…. but you need to know how to use custom listview and adapter Commented Nov 1, 2013 at 8:36
  • you can make in the getView method of the custom adapter and add the dynamic layout what you want by making a switch case in it... Commented Nov 1, 2013 at 10:54

2 Answers 2

9

You need to extend your Adapter, and override its getView method.

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int resource;

    // Here you set ‘resource’ with the correct layout, for the row
    // given by the parameter ‘position.’
    //
    // E.g.:
    //
    // switch (someArray[position].type) {
    //   case SOME_TYPE_A: resource = R.layout.a; break;
    //   case SOME_TYPE_B: resource = R.layout.b; break;
    //   ...
    // }

    View rowView = inflater.inflate(resource, parent, false);

    // Here you initialize the contents of the newly created view.
    //
    // E.g.:
    // switch (resource) {
    //   case R.layout.a:
    //      TextView aA = (TextView) rowView.findViewById(R.id.aa);
    //      aA.setText("View 1");
    //      ...
    //      break;
    //   case R.layout.b:
    //      TextView bB = (TextView) rowView.findViewById(R.id.bb);
    //      bB.setText("View 2");
    //      ...
    //      break;
    //   ...
    // }

    return rowView;
}

For more examples on adapters and how to extend them, see the links below.

Sign up to request clarification or add additional context in comments.

Comments

7

create regular adapter , in the create_view function inflate the row xml layout according to the row type.

for example

@Override   
public View getView(int position, View convertView, ViewGroup parent) {
     LayoutInflater inflater = (LayoutInflater) context
         .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     if (position % 2 == 0 )
        xml_type = R.layout.row_one
     else
         xml_type = R.layout.row_two

     View rowView = inflater.inflate(xml_type, parent, false);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.