package com.activeandroid.widget;

import java.util.List;

import android.content.Context;
import android.widget.ArrayAdapter;

import com.activeandroid.Model;

public class ModelAdapter<T extends Model> extends ArrayAdapter<T> {

	public ModelAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
	}

	public ModelAdapter(Context context, int resource,
			int textViewResourceId) {
		super(context, resource, textViewResourceId);
	}

	public ModelAdapter(Context context, int textViewResourceId,
			List<T> objects) {
		super(context, textViewResourceId, objects);
	}

	public ModelAdapter(Context context, int resource,
			int textViewResourceId, List<T> objects) {
		super(context, resource, textViewResourceId, objects);
	}

	/**
	 * Clears the adapter and, if data != null, fills if with new Items.
	 * 
	 * @param data A List<T> which members get added to the adapter.
	 */
	public void setData(List<T> data) {
	    clear();
	    if (data != null) {
	        for (T t : data) {
				add(t);
			}
	    }
	}

	/**
	 * @throws RuntimeException If no record is found.
	 * @return The Id of the record at position.
	 */
	@Override
	public long getItemId(int position) {
		T t = this.getItem(position);
		if (t!=null)
			return t.getId();
		else
			throw new RuntimeException("ItemNotfound");
	}

}

@@ -24,24 +24,53 @@
 id.widget;%0A%0A
+import java.util.Collection;%0A
 import java.
@@ -252,17 +252,16 @@
 er%3CT%3E %7B%0A
-%0A
 %09public 
@@ -401,36 +401,33 @@
 t, int resource,
-%0A%09%09%09
+ 
 int textViewReso
@@ -549,20 +549,17 @@
 ourceId,
-%0A%09%09%09
+ 
 List%3CT%3E 
@@ -671,20 +671,17 @@
 esource,
-%0A%09%09%09
+ 
 int text
@@ -871,20 +871,42 @@
 ram 
-data A List%3C
+collection A Collection%3C? extends 
 T%3E w
@@ -974,29 +974,48 @@
 ata(
-List%3CT%3E data
+Collection%3C? extends T%3E collection
 ) %7B%0A%09
-    
+%09
 clea
@@ -1023,57 +1023,64 @@
 ();%0A
-%09    if (data != null) %7B%0A%09        for (T t : data
+%0A%09%09if (collection != null) %7B%0A%09%09%09for (T item : collection
 ) %7B%0A
@@ -1087,17 +1087,20 @@
 %09%09%09%09add(
-t
+item
 );%0A%09%09%09%7D%0A
@@ -1104,12 +1104,9 @@
 %09%7D%0A%09
-    
+%09
 %7D%0A%09%7D
@@ -1116,60 +1116,8 @@
 /**%0A
-%09 * @throws RuntimeException If no record is found.%0A
 %09 * 
@@ -1221,17 +1221,15 @@
 %09%09T 
-t = this.
+item = 
 getI
@@ -1247,22 +1247,30 @@
 n);%0A
+%0A
 %09%09if (
-t
+item 
 !=
+ 
 null)
+ %7B
 %0A%09%09%09
@@ -1276,17 +1276,20 @@
 %09return 
-t
+item
 .getId()
@@ -1294,64 +1294,39 @@
 ();%0A
+%09%09%7D%0A
 %09%09else
+ %7B
 %0A%09%09%09
-throw new RuntimeException(%22ItemNotfound%22)
+return -1
 ;%0A
+%09
 %09%7D%0A
-%0A
+%09
 %7D%0A
+%7D

Format police. getItemId() fails silently. setData conforms to Android standard.