Android UI How to - Use custom model in ArrayAdapter








The following code shows how to use custom model in ArrayAdapter.

Example

res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (c) 2012 Manning
  See the file license.txt for copying permission.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/image"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>

Java code

/*******************************************************************************
 * Copyright (c) 2012 Manning//  w  w  w . java2s  . c  o  m
 * See the file license.txt for copying permission.
 ******************************************************************************/
import java.util.List;
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;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;

public class MainActivity extends ListActivity {

  private static final int MODEL_COUNT = 30;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ModelAdapter(this, 0, buildModels()));
  }

  private List<Model> buildModels() {
    final ArrayList<Model> ret = new ArrayList<Model>(MODEL_COUNT);
    for (int i = 0; i < MODEL_COUNT; i++) {
      final Model model = new Model();
      model.setImage(R.drawable.icon);
      model.setText1("Name " + i);
      model.setText2("Description " + i);
      ret.add(model);
    }
    return ret;
  }
}
class Model {

  private String mText1;
  private String mText2;
  private int mImageResId;

  public String getText1() {
    return mText1;
  }

  public void setText1(String text1) {
    mText1 = text1;
  }

  public String getText2() {
    return mText2;
  }

  public void setText2(String text2) {
    mText2 = text2;
  }

  public int getImage() {
    return mImageResId;
  }

  public void setImage(int imageResId) {
    mImageResId = imageResId;
  }
}

class ModelAdapter extends ArrayAdapter<Model> {

  private LayoutInflater mInflater;

  public ModelAdapter(Context context, int textViewResourceId,
      List<Model> objects) {
    super(context, textViewResourceId, objects);
    mInflater = LayoutInflater.from(context);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;

    if (convertView == null) {
      convertView = mInflater.inflate(R.layout.row_layout, parent,
          false);

      viewHolder = new ViewHolder();
      viewHolder.imageView = (ImageView) convertView
          .findViewById(R.id.image);
      viewHolder.text1 = (TextView) convertView
          .findViewById(R.id.text1);
      viewHolder.text2 = (TextView) convertView
          .findViewById(R.id.text2);

      convertView.setTag(viewHolder);

    } else {
      viewHolder = (ViewHolder) convertView.getTag();
    }

    Model model = getItem(position);
    viewHolder.imageView.setImageResource(model.getImage());
    viewHolder.text1.setText(model.getText1());
    viewHolder.text2.setText(model.getText2());

    return convertView;
  }

  private static class ViewHolder {
    public ImageView imageView;
    public TextView text1;
    public TextView text2;
  }
}

res/layout/row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView android:id="@+id/numbers_row_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"/>

    <Button android:id="@+id/numbers_row_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Remove"/>

</LinearLayout>