SimpleCursorTreeAdapterpublic abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter An easy adapter to map columns from a cursor to TextViews or ImageViews
defined in an XML file. You can specify which columns you want, which views
you want to display the columns, and the XML file that defines the appearance
of these views. Separate XML files for child and groups are possible.
Binding occurs in two phases. First, if a
{@link android.widget.SimpleCursorTreeAdapter.ViewBinder} is available,
{@link ViewBinder#setViewValue(android.view.View, android.database.Cursor, int)}
is invoked. If the returned value is true, binding has occurred. If the
returned value is false and the view to bind is a TextView,
{@link #setViewText(TextView, String)} is invoked. If the returned value
is false and the view to bind is an ImageView,
{@link #setViewImage(ImageView, String)} is invoked. If no appropriate
binding can be found, an {@link IllegalStateException} is thrown. |
Fields Summary |
---|
private String[] | mGroupFromNamesThe name of the columns that contain the data to display for a group. | private int[] | mGroupFromThe indices of columns that contain data to display for a group. | private int[] | mGroupToThe View IDs that will display a group's data fetched from the
corresponding column. | private String[] | mChildFromNamesThe name of the columns that contain the data to display for a child. | private int[] | mChildFromThe indices of columns that contain data to display for a child. | private int[] | mChildToThe View IDs that will display a child's data fetched from the
corresponding column. | private ViewBinder | mViewBinderView binder, if supplied |
Constructors Summary |
---|
public SimpleCursorTreeAdapter(android.content.Context context, android.database.Cursor cursor, int collapsedGroupLayout, int expandedGroupLayout, String[] groupFrom, int[] groupTo, int childLayout, int lastChildLayout, String[] childFrom, int[] childTo)Constructor.
super(context, cursor, collapsedGroupLayout, expandedGroupLayout, childLayout,
lastChildLayout);
init(groupFrom, groupTo, childFrom, childTo);
| public SimpleCursorTreeAdapter(android.content.Context context, android.database.Cursor cursor, int collapsedGroupLayout, int expandedGroupLayout, String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom, int[] childTo)Constructor.
super(context, cursor, collapsedGroupLayout, expandedGroupLayout, childLayout);
init(groupFrom, groupTo, childFrom, childTo);
| public SimpleCursorTreeAdapter(android.content.Context context, android.database.Cursor cursor, int groupLayout, String[] groupFrom, int[] groupTo, int childLayout, String[] childFrom, int[] childTo)Constructor.
super(context, cursor, groupLayout, childLayout);
init(groupFrom, groupTo, childFrom, childTo);
|
Methods Summary |
---|
protected void | bindChildView(android.view.View view, android.content.Context context, android.database.Cursor cursor, boolean isLastChild)
if (mChildFrom == null) {
mChildFrom = new int[mChildFromNames.length];
initFromColumns(cursor, mChildFromNames, mChildFrom);
}
bindView(view, context, cursor, mChildFrom, mChildTo);
| protected void | bindGroupView(android.view.View view, android.content.Context context, android.database.Cursor cursor, boolean isExpanded)
if (mGroupFrom == null) {
mGroupFrom = new int[mGroupFromNames.length];
initFromColumns(cursor, mGroupFromNames, mGroupFrom);
}
bindView(view, context, cursor, mGroupFrom, mGroupTo);
| private void | bindView(android.view.View view, android.content.Context context, android.database.Cursor cursor, int[] from, int[] to)
final ViewBinder binder = mViewBinder;
for (int i = 0; i < to.length; i++) {
View v = view.findViewById(to[i]);
if (v != null) {
boolean bound = false;
if (binder != null) {
bound = binder.setViewValue(v, cursor, from[i]);
}
if (!bound) {
String text = cursor.getString(from[i]);
if (text == null) {
text = "";
}
if (v instanceof TextView) {
setViewText((TextView) v, text);
} else if (v instanceof ImageView) {
setViewImage((ImageView) v, text);
} else {
throw new IllegalStateException("SimpleCursorTreeAdapter can bind values" +
" only to TextView and ImageView!");
}
}
}
}
| public android.widget.SimpleCursorTreeAdapter$ViewBinder | getViewBinder()Returns the {@link ViewBinder} used to bind data to views.
return mViewBinder;
| private void | init(java.lang.String[] groupFromNames, int[] groupTo, java.lang.String[] childFromNames, int[] childTo)
mGroupFromNames = groupFromNames;
mGroupTo = groupTo;
mChildFromNames = childFromNames;
mChildTo = childTo;
| private void | initFromColumns(android.database.Cursor cursor, java.lang.String[] fromColumnNames, int[] fromColumns)
for (int i = fromColumnNames.length - 1; i >= 0; i--) {
fromColumns[i] = cursor.getColumnIndexOrThrow(fromColumnNames[i]);
}
| public void | setViewBinder(android.widget.SimpleCursorTreeAdapter$ViewBinder viewBinder)Sets the binder used to bind data to views.
mViewBinder = viewBinder;
| protected void | setViewImage(ImageView v, java.lang.String value)Called by bindView() to set the image for an ImageView. By default, the
value will be treated as a Uri. Intended to be overridden by Adapters
that need to filter strings retrieved from the database.
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 a TextView.
Intended to be overridden by Adapters that need to filter strings
retrieved from the database.
v.setText(text);
|
|