FileDocCategorySizeDatePackage
SimpleAdapter.javaAPI DocAndroid 1.5 API14332Wed May 06 22:41:56 BST 2009android.widget

SimpleAdapter

public class SimpleAdapter extends BaseAdapter implements Filterable
An easy adapter to map static data to views defined in an XML file. You can specify the data backing the list as an ArrayList of Maps. Each entry in the ArrayList corresponds to one row in the list. The Maps contain the data for each row. You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views. Binding data to views occurs in two phases. First, if a {@link android.widget.SimpleAdapter.ViewBinder} is available, {@link ViewBinder#setViewValue(android.view.View, Object, String)} is invoked. If the returned value is true, binding has occurred. If the returned value is false, the following views are then tried in order:
  • A view that implements Checkable (e.g. CheckBox). The expected bind value is a boolean.
  • TextView. The expected bind value is a string and {@link #setViewText(TextView, String)} is invoked.
  • ImageView. The expected bind value is a resource id or a string and {@link #setViewImage(ImageView, int)} or {@link #setViewImage(ImageView, String)} is invoked.
If no appropriate binding can be found, an {@link IllegalStateException} is thrown.

Fields Summary
private int[]
mTo
private String[]
mFrom
private ViewBinder
mViewBinder
private List
mData
private int
mResource
private int
mDropDownResource
private android.view.LayoutInflater
mInflater
private SimpleFilter
mFilter
private ArrayList
mUnfilteredData
Constructors Summary
public SimpleAdapter(android.content.Context context, List data, int resource, String[] from, int[] to)
Constructor

param
context The context where the View associated with this SimpleAdapter is running
param
data A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
param
resource Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
param
from A list of column names that will be added to the Map associated with each item.
param
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

        mData = data;
        mResource = mDropDownResource = resource;
        mFrom = from;
        mTo = to;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
Methods Summary
private voidbindView(int position, android.view.View view)

        final Map dataSet = mData.get(position);
        if (dataSet == null) {
            return;
        }

        final ViewBinder binder = mViewBinder;
        final View[] holder = (View[]) view.getTag();
        final String[] from = mFrom;
        final int[] to = mTo;
        final int count = to.length;

        for (int i = 0; i < count; i++) {
            final View v = holder[i];
            if (v != null) {
                final Object data = dataSet.get(from[i]);
                String text = data == null ? "" : data.toString();
                if (text == null) {
                    text = "";
                }

                boolean bound = false;
                if (binder != null) {
                    bound = binder.setViewValue(v, data, text);
                }

                if (!bound) {
                    if (v instanceof Checkable) {
                        if (data instanceof Boolean) {
                            ((Checkable) v).setChecked((Boolean) data);
                        } else {
                            throw new IllegalStateException(v.getClass().getName() +
                                    " should be bound to a Boolean, not a " + data.getClass());
                        }
                    } else if (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the bottom of these
                        // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                        setViewText((TextView) v, text);
                    } else if (v instanceof ImageView) {
                        if (data instanceof Integer) {
                            setViewImage((ImageView) v, (Integer) data);                            
                        } else {
                            setViewImage((ImageView) v, text);
                        }
                    } else {
                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleAdapter");
                    }
                }
            }
        }
    
private android.view.ViewcreateViewFromResource(int position, android.view.View convertView, android.view.ViewGroup parent, int resource)

        View v;
        if (convertView == null) {
            v = mInflater.inflate(resource, parent, false);

            final int[] to = mTo;
            final int count = to.length;
            final View[] holder = new View[count];

            for (int i = 0; i < count; i++) {
                holder[i] = v.findViewById(to[i]);
            }

            v.setTag(holder);
        } else {
            v = convertView;
        }

        bindView(position, v);

        return v;
    
public intgetCount()

see
android.widget.Adapter#getCount()

        return mData.size();
    
public android.view.ViewgetDropDownView(int position, android.view.View convertView, android.view.ViewGroup parent)

        return createViewFromResource(position, convertView, parent, mDropDownResource);
    
public FiltergetFilter()

        if (mFilter == null) {
            mFilter = new SimpleFilter();
        }
        return mFilter;
    
public java.lang.ObjectgetItem(int position)

see
android.widget.Adapter#getItem(int)

        return mData.get(position);
    
public longgetItemId(int position)

see
android.widget.Adapter#getItemId(int)

        return position;
    
public android.view.ViewgetView(int position, android.view.View convertView, android.view.ViewGroup parent)

see
android.widget.Adapter#getView(int, View, ViewGroup)

        return createViewFromResource(position, convertView, parent, mResource);
    
public android.widget.SimpleAdapter$ViewBindergetViewBinder()
Returns the {@link ViewBinder} used to bind data to views.

return
a ViewBinder or null if the binder does not exist
see
#setViewBinder(android.widget.SimpleAdapter.ViewBinder)

        return mViewBinder;
    
public voidsetDropDownViewResource(int resource)

Sets the layout resource to create the drop down views.

param
resource the layout resource defining the drop down views
see
#getDropDownView(int, android.view.View, android.view.ViewGroup)

        this.mDropDownResource = resource;
    
public voidsetViewBinder(android.widget.SimpleAdapter$ViewBinder viewBinder)
Sets the binder used to bind data to views.

param
viewBinder the binder used to bind data to views, can be null to remove the existing binder
see
#getViewBinder()

        mViewBinder = viewBinder;
    
public voidsetViewImage(ImageView v, int value)
Called by bindView() to set the image for an ImageView but only if there is no existing ViewBinder or if the existing ViewBinder cannot handle binding to an ImageView. This method is called instead of {@link #setViewImage(ImageView, String)} if the supplied data is an int or Integer.

param
v ImageView to receive an image
param
value the value retrieved from the data set
see
#setViewImage(ImageView, String)

        v.setImageResource(value);
    
public voidsetViewImage(ImageView v, java.lang.String value)
Called by bindView() to set the image for an ImageView but only if there is no existing ViewBinder or if the existing ViewBinder cannot handle binding to an ImageView. By default, the value will be treated as an image resource. If the value cannot be used as an image resource, the value is used as an image Uri. This method is called instead of {@link #setViewImage(ImageView, int)} if the supplied data is not an int or Integer.

param
v ImageView to receive an image
param
value the value retrieved from the data set
see
#setViewImage(ImageView, int)

        try {
            v.setImageResource(Integer.parseInt(value));
        } catch (NumberFormatException nfe) {
            v.setImageURI(Uri.parse(value));
        }
    
public voidsetViewText(TextView v, java.lang.String text)
Called by bindView() to set the text for a TextView but only if there is no existing ViewBinder or if the existing ViewBinder cannot handle binding to an TextView.

param
v TextView to receive text
param
text the text to be set for the TextView

        v.setText(text);