2

I have data from a WS that need to be shown on an Android ListView. Data can be of different types, so i need to dynamically create a layout for ListView items, how can i do it? I think i have to use LayoutInflater but have always used it with existing layouts and never created a layout from scratch. How can i do it?

Cris
  • 12,124
  • 27
  • 92
  • 159

2 Answers2

2

you need to create adapter for that

just check this link it elaborate all about using adapters.

http://www.vogella.de/articles/AndroidListView/article.html

// edited

in the given url just check this code

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MySimpleArrayAdapter(Context context, String[] values) {
        super(context, R.layout.rowlayout, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        textView.setText(values[position]);
        // Change the icon for Windows and iPhone
        String s = values[position];
        if (s.startsWith("iPhone")) {
            imageView.setImageResource(R.drawable.no);
        } else {
            imageView.setImageResource(R.drawable.ok);
        }

        return rowView;
    }
}

where R.layout.rowlayout is the layout for your each row in which you can define your imageviews,textviews

vipin
  • 2,851
  • 4
  • 18
  • 32
  • vogella site has great tutorials but cannot find an example with a layout created from scratch where views are add added; all the examples i see use a pre-existing xml layout that is inflated. I wanted to avoid this. – Cris Apr 02 '12 at 12:53
  • ok then why dont you try to create them by new object as ListView list = new ListView(context)); – vipin Apr 02 '12 at 12:54
  • No, because i don't need to add a new ListView but, inside the ListView for every row, i need to add items as TextView or ImageView. I think to have to used something similar to your solution in getView but have not idea on how to do it. – Cris Apr 02 '12 at 14:10
1

As you mention you're using different datatypes, I assume you want to show different layouts per datatype. It's not necessary to dynamically create your listview-items; if you want, you can just inflate them from XML.

Create a custom listadapter (see the link to vogella provided by vipin) and override the getView(), getItemViewType() and getViewTypeCount() methods.

In getView() you build your views either programmatically or inflate them from XML. To determine which layout to inflate (or build), call getItemViewType(position), check the value it returns and then choose which layout to inflate for that value. For more info on how to build XML-layouts, see this page in the dev guide on XML layouts.

Next, take a look at this answer and implement this into your adapter. Be sure to also read the comments. For example, override getItemViewType() with something like:

public int getItemViewType(int position) {

    if(getItem(position) instanceOf ItemA) {
        return 0;
    } else {
        return 1;
    }
}

This approach allows your listview to recycle views and use the ViewHolder-pattern (for more info, again see the link vipin provided).

Community
  • 1
  • 1
Reinier
  • 3,836
  • 1
  • 27
  • 28