Android UI How to - ListView with custom Row








The following code shows how to ListView with custom Row.

Example

Layout xml

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

    <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

        <EditText android:id="@+id/main_edittext"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:hint="Write a number"
            android:inputType="number"
            android:singleLine="true"/>

        <Button android:id="@+id/main_add"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="Add"
            android:onClick="addNumber"/>
    </LinearLayout>

    <ListView android:id="@+id/main_listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

res/layout/number_row.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
  Copyright (c) 2012 Manning
  See the file license.txt for copying permission.
-->
<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>

Java code

/*******************************************************************************
 * Copyright (c) 2012 Manning/*  www. j  a va 2 s. co m*/
 * See the file license.txt for copying permission.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements
    NumbersAdapterDelegate {
  private static final String TAG = MainActivity.class
      .getCanonicalName();

  private ListView mListView;
  private ArrayList<Integer> mNumbers;
  private NumbersAdapter mAdapter;
  private EditText mEditText;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mEditText = (EditText) findViewById(R.id.main_edittext);
    mListView = (ListView) findViewById(R.id.main_listview);
    mNumbers = new ArrayList<Integer>();
    mAdapter = new NumbersAdapter(this, mNumbers);
    mListView.setAdapter(mAdapter);
  }

  @Override
  protected void onResume() {
    super.onResume();
    mAdapter.setDelegate(this);
  }

  @Override
  protected void onPause() {
    super.onPause();
    mAdapter.setDelegate(null);
  }

  @Override
  public void removeItem(Integer value) {
    mNumbers.remove(value);
    Toast
        .makeText(this, "Removed object: " + value, Toast.LENGTH_SHORT)
        .show();
    mAdapter.notifyDataSetChanged();
  }

  public void addNumber(View v) {
    String value = mEditText.getText().toString().trim();
    try {
      mNumbers.add(Integer.valueOf(value));
      mEditText.setText("");
      mAdapter.notifyDataSetChanged();
    } catch (NumberFormatException e) {
      Log.e(TAG, "Couldn't convert to integer the string: " + value);
    }
  }
}
interface NumbersAdapterDelegate {
    void removeItem(Integer value);
}
class NumbersAdapter extends ArrayAdapter<Integer> {
  private LayoutInflater mInflator;
  private NumbersAdapterDelegate mDelegate;

  public NumbersAdapter(Context context, List<Integer> objects) {
    super(context, 0, objects);
    mInflator = LayoutInflater.from(context);
  }

  @Override
  public View getView(int position, View cv, ViewGroup parent) {

    if (null == cv) {
      cv = mInflator.inflate(R.layout.number_row, parent, false);
    }

    final Integer value = getItem(position);
    TextView tv = (TextView) cv.findViewById(R.id.numbers_row_text);
    tv.setText(value.toString());

    View button = cv.findViewById(R.id.numbers_row_button);
    button.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        if (null != mDelegate) {
          mDelegate.removeItem(value);
        }
      }
    });

    return cv;
  }

  public void setDelegate(NumbersAdapterDelegate delegate) {
    mDelegate = delegate;
  }

}
null