Populate listview from arraylist of objects

djargonforce picture djargonforce · Mar 8, 2013 · Viewed 50.8k times · Source

I have a listactivity which will display a list of persons name and address with data from arraylist of objects. here's the method to fill the listview so far..

private void fillData(ArrayList<Person> messages) {
    //populating list should go here
}

The person class is stores name and address of a person.

public class Person {
   String name;
   String address;
}

While my listitem for my listview consist of two textview, like this :

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:mode="twoLine"
android:clickable="false"
android:paddingBottom="9dp"
android:paddingTop="5dp" >

<TextView
    android:id="@+id/display_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/display_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/display_name"
    android:layout_below="@+id/display_name"
    android:textAppearance="?android:attr/textAppearanceSmall" />

I want each item in my listview to displaying both the name and address of each person. Can somebody please help ?

Thanks in advance, and sorry for my bad English...

Answer

Talha picture Talha · Mar 8, 2013

In your activity

AdapterPerson adbPerson;
ArrayList<Person> myListItems  = new ArrayList<Person>();

//then populate myListItems  

adbPerson= new AdapterPerson (youractivity.this, 0, myListItems);
listview.setAdapter(adbPerson);

Adapter

public class AdapterPerson extends ArrayAdapter<Person> {
    private Activity activity;
    private ArrayList<Person> lPerson;
    private static LayoutInflater inflater = null;

    public AdapterPerson (Activity activity, int textViewResourceId,ArrayList<Person> _lPerson) {
        super(activity, textViewResourceId, _lProducts);
        try {
            this.activity = activity;
            this.lPerson = _lPerson;

            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        } catch (Exception e) {

        }
    }

    public int getCount() {
        return lPerson.size();
    }

    public Product getItem(Product position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView display_name;
        public TextView display_number;             

    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        final ViewHolder holder;
        try {
            if (convertView == null) {
                vi = inflater.inflate(R.layout.yourlayout, null);
                holder = new ViewHolder();

                holder.display_name = (TextView) vi.findViewById(R.id.display_name);
                holder.display_number = (TextView) vi.findViewById(R.id.display_number);


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



            holder.display_name.setText(lProducts.get(position).name);
            holder.display_number.setText(lProducts.get(position).number);


        } catch (Exception e) {


        }
        return vi;
    }
}