SimpleCursorAdapterpublic class SimpleCursorAdapter extends ResourceCursorAdapter 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.
Binding occurs in two phases. First, if a
{@link android.widget.SimpleCursorAdapter.ViewBinder} is available,
{@link ViewBinder#setViewValue(android.view.View, android.database.Cursor, int)}
is invoked. If the returned value is true, binding has occured. 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.
If this adapter is used with filtering, for instance in an
{@link android.widget.AutoCompleteTextView}, you can use the
{@link android.widget.SimpleCursorAdapter.CursorToStringConverter} and the
{@link android.widget.FilterQueryProvider} interfaces
to get control over the filtering process. You can refer to
{@link #convertToString(android.database.Cursor)} and
{@link #runQueryOnBackgroundThread(CharSequence)} for more information. |
Fields Summary |
---|
protected int[] | mFromA list of columns containing the data to bind to the UI.
This field should be made private, so it is hidden from the SDK.
{@hide} | protected int[] | mToA list of View ids representing the views to which the data must be bound.
This field should be made private, so it is hidden from the SDK.
{@hide} | private int | mStringConversionColumn | private CursorToStringConverter | mCursorToStringConverter | private ViewBinder | mViewBinder | private String[] | mOriginalFrom |
Methods Summary |
---|
public void | bindView(android.view.View view, android.content.Context context, android.database.Cursor cursor)Binds all of the field names passed into the "to" parameter of the
constructor with their corresponding cursor columns as specified in the
"from" parameter.
Binding occurs in two phases. First, if a
{@link android.widget.SimpleCursorAdapter.ViewBinder} is available,
{@link ViewBinder#setViewValue(android.view.View, android.database.Cursor, int)}
is invoked. If the returned value is true, binding has occured. 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.
final View[] holder = (View[]) view.getTag();
final ViewBinder binder = mViewBinder;
final int count = mTo.length;
final int[] from = mFrom;
for (int i = 0; i < count; i++) {
final View v = holder[i];
if (v != null) {
String text = cursor.getString(from[i]);
if (text == null) {
text = "";
}
boolean bound = false;
if (binder != null) {
bound = binder.setViewValue(v, cursor, from[i]);
}
if (!bound) {
if (v instanceof TextView) {
setViewText((TextView) v, text);
} else if (v instanceof ImageView) {
setViewImage((ImageView) v, text);
} else {
throw new IllegalStateException(v.getClass().getName() + " is not a " +
" view that can be bounds by this SimpleCursorAdapter");
}
}
}
}
| public void | changeCursor(android.database.Cursor c)
super.changeCursor(c);
// rescan columns in case cursor layout is different
findColumns(mOriginalFrom);
| public void | changeCursorAndColumns(android.database.Cursor c, java.lang.String[] from, int[] to)Change the cursor and change the column-to-view mappings at the same time.
mOriginalFrom = from;
mTo = to;
super.changeCursor(c);
findColumns(mOriginalFrom);
| public java.lang.CharSequence | convertToString(android.database.Cursor cursor)Returns a CharSequence representation of the specified Cursor as defined
by the current CursorToStringConverter. If no CursorToStringConverter
has been set, the String conversion column is used instead. If the
conversion column is -1, the returned String is empty if the cursor
is null or Cursor.toString().
if (mCursorToStringConverter != null) {
return mCursorToStringConverter.convertToString(cursor);
} else if (mStringConversionColumn > -1) {
return cursor.getString(mStringConversionColumn);
}
return super.convertToString(cursor);
| private void | findColumns(java.lang.String[] from)Create a map from an array of strings to an array of column-id integers in mCursor.
If mCursor is null, the array will be discarded.
if (mCursor != null) {
int i;
int count = from.length;
if (mFrom == null || mFrom.length != count) {
mFrom = new int[count];
}
for (i = 0; i < count; i++) {
mFrom[i] = mCursor.getColumnIndexOrThrow(from[i]);
}
} else {
mFrom = null;
}
| private android.view.View | generateViewHolder(android.view.View v)
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);
return v;
| public android.widget.SimpleCursorAdapter$CursorToStringConverter | getCursorToStringConverter()Returns the converter used to convert the filtering Cursor
into a String.
return mCursorToStringConverter;
| public int | getStringConversionColumn()Return the index of the column used to get a String representation
of the Cursor.
return mStringConversionColumn;
| public android.widget.SimpleCursorAdapter$ViewBinder | getViewBinder()Returns the {@link ViewBinder} used to bind data to views.
return mViewBinder;
| public android.view.View | newDropDownView(android.content.Context context, android.database.Cursor cursor, android.view.ViewGroup parent)
return generateViewHolder(super.newDropDownView(context, cursor, parent));
| public android.view.View | newView(android.content.Context context, android.database.Cursor cursor, android.view.ViewGroup parent)
return generateViewHolder(super.newView(context, cursor, parent));
| public void | setCursorToStringConverter(android.widget.SimpleCursorAdapter$CursorToStringConverter cursorToStringConverter)Sets the converter used to convert the filtering Cursor
into a String.
mCursorToStringConverter = cursorToStringConverter;
| public void | setStringConversionColumn(int stringConversionColumn)Defines the index of the column in the Cursor used to get a String
representation of that Cursor. The column is used to convert the
Cursor to a String only when the current CursorToStringConverter
is null.
mStringConversionColumn = stringConversionColumn;
| public void | setViewBinder(android.widget.SimpleCursorAdapter$ViewBinder viewBinder)Sets the binder used to bind data to views.
mViewBinder = viewBinder;
| 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.
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 an TextView.
Intended to be overridden by Adapters that need to filter strings
retrieved from the database.
v.setText(text);
|
|