Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to update multiple TextView with ArrayAdapter?

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));
like image 759
birdy Avatar asked Jan 08 '14 02:01

birdy


1 Answers

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.

like image 70
Amulya Khare Avatar answered Nov 10 '22 08:11

Amulya Khare