Question

I have a list view that has multiple textview's like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16dip"
        android:textColor="#000000"
        android:paddingLeft="10dip"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/address"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16dip"
        android:textColor="#000000"
        android:paddingTop="15dip"
        android:paddingBottom="15dip"
        android:paddingLeft="10dip"
        android:textStyle="bold"/>
</RelativeLayout>

I have a list of POJO's that has a name and address and I want each item in the list view to be populated with those values.

My POJO is like this:

public class Person {
  private String name;
  private String address;
  //getter setter
  public String toString() {return name;} 
}

Question

When I set the list adapter with my list how can I set both name and address?

Currently I'm doing this which is only setting the name:

setListAdapter(new ArrayAdapter<Person>(MyActivity.this, R.layout.list_text, R.id.name, personList));
Was it helpful?

Solution

You should create a custom adapter that extends ArrayAdapter. The things you can do with an ArrayAdapter are limited.

Something like this:

public class PersonAdapter extends ArrayAdapter<Person> {
    private final Context context;
    private final ArrayList<Person> data;
    private final int layoutResourceId;

    public PersonAdapter(Context context, int layoutResourceId, ArrayList<Person> data) {
        super(context, layoutResourceId, data);
        this.context = context;
        this.data = data;
        this.layoutResourceId = layoutResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ViewHolder();
            holder.textView1 = (TextView)row.findViewById(R.id.text1);
            holder.textView2 = (TextView)row.findViewById(R.id.text2);
            ...
            ...
            holder.textView3 = (TextView)row.findViewById(R.id.text3);

            row.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)row.getTag();
        }

        Person person = data.get(position);

        holder.textView1.setText(person.getName());
        holder.textView2.setText(person.getAddress());
        ...
        ...
        holder.textView3.setText(person.getEtc());

        return row;
    }

    static class ViewHolder
    {
        TextView textView1;
        TextView textView2;
        ...
        ...
        TextView textView3;
    }
}

Where textView1, textView2 ... textView-n are all your text views. Set your adapter as follows:

setListAdapter(new PersonAdapter(MyActivity.this, R.layout.list_text, personList));

Note: I assume that your personList is a List type object. If it is an Array do let me know.

OTHER TIPS

I would check out the TwoLineArrayAdapter by ESV, it's formatted for name and title but is almost exactly what you need.

Edit:

class StringHolder {
    public String text;
    public int viewId;
}

For a scenario of n strings, the solution is very similar. Create a custom ArrayAdapter of type StringHolder[] or List<StringHolder> depending on if this amount if fixed. In your getView you would loop through all StringHolder objects, call findViewById, and update the text content. Alternatively, you could simply use the index in the array/ list and map that to a view ID if they will remain consistent.

This is of course just one way to do this, there are many more and I'll leave the actual implementation as an exercise for the reader.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top