SimpleAdapterpublic class SimpleAdapter extends BaseAdapter implements FilterableAn 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
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
Methods Summary |
---|
private void | bindView(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.View | createViewFromResource(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 int | getCount()
return mData.size();
| public android.view.View | getDropDownView(int position, android.view.View convertView, android.view.ViewGroup parent)
return createViewFromResource(position, convertView, parent, mDropDownResource);
| public Filter | getFilter()
if (mFilter == null) {
mFilter = new SimpleFilter();
}
return mFilter;
| public java.lang.Object | getItem(int position)
return mData.get(position);
| public long | getItemId(int position)
return position;
| public android.view.View | getView(int position, android.view.View convertView, android.view.ViewGroup parent)
return createViewFromResource(position, convertView, parent, mResource);
| public android.widget.SimpleAdapter$ViewBinder | getViewBinder()Returns the {@link ViewBinder} used to bind data to views.
return mViewBinder;
| public void | setDropDownViewResource(int resource)Sets the layout resource to create the drop down views.
this.mDropDownResource = resource;
| public void | setViewBinder(android.widget.SimpleAdapter$ViewBinder viewBinder)Sets the binder used to bind data to views.
mViewBinder = viewBinder;
| public void | setViewImage(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.
v.setImageResource(value);
| public void | setViewImage(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.
try {
v.setImageResource(Integer.parseInt(value));
} catch (NumberFormatException nfe) {
v.setImageURI(Uri.parse(value));
}
| public void | setViewText(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.
v.setText(text);
|
|