FileDocCategorySizeDatePackage
SimpleCursorTreeAdapter.javaAPI DocAndroid 5.1 API14575Thu Mar 12 22:22:10 GMT 2015android.widget

SimpleCursorTreeAdapter

public 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[]
mGroupFromNames
The name of the columns that contain the data to display for a group.
private int[]
mGroupFrom
The indices of columns that contain data to display for a group.
private int[]
mGroupTo
The View IDs that will display a group's data fetched from the corresponding column.
private String[]
mChildFromNames
The name of the columns that contain the data to display for a child.
private int[]
mChildFrom
The indices of columns that contain data to display for a child.
private int[]
mChildTo
The View IDs that will display a child's data fetched from the corresponding column.
private ViewBinder
mViewBinder
View 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.

param
context The context where the {@link ExpandableListView} associated with this {@link SimpleCursorTreeAdapter} is running
param
cursor The database cursor
param
collapsedGroupLayout The resource identifier of a layout file that defines the views for a collapsed group. The layout file should include at least those named views defined in groupTo.
param
expandedGroupLayout The resource identifier of a layout file that defines the views for an expanded group. The layout file should include at least those named views defined in groupTo.
param
groupFrom A list of column names that will be used to display the data for a group.
param
groupTo The group views (from the group layouts) that should display column in the "from" parameter. These should all be TextViews or ImageViews. The first N views in this list are given the values of the first N columns in the from parameter.
param
childLayout The resource identifier of a layout file that defines the views for a child (except the last). The layout file should include at least those named views defined in childTo.
param
lastChildLayout The resource identifier of a layout file that defines the views for the last child within a group. The layout file should include at least those named views defined in childTo.
param
childFrom A list of column names that will be used to display the data for a child.
param
childTo The child views (from the child layouts) that should display column in the "from" parameter. These should all be TextViews or ImageViews. The first N views in this list are given the values of the first N columns in the from parameter.

        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.

param
context The context where the {@link ExpandableListView} associated with this {@link SimpleCursorTreeAdapter} is running
param
cursor The database cursor
param
collapsedGroupLayout The resource identifier of a layout file that defines the views for a collapsed group. The layout file should include at least those named views defined in groupTo.
param
expandedGroupLayout The resource identifier of a layout file that defines the views for an expanded group. The layout file should include at least those named views defined in groupTo.
param
groupFrom A list of column names that will be used to display the data for a group.
param
groupTo The group views (from the group layouts) that should display column in the "from" parameter. These should all be TextViews or ImageViews. The first N views in this list are given the values of the first N columns in the from parameter.
param
childLayout The resource identifier of a layout file that defines the views for a child. The layout file should include at least those named views defined in childTo.
param
childFrom A list of column names that will be used to display the data for a child.
param
childTo The child views (from the child layouts) that should display column in the "from" parameter. These should all be TextViews or ImageViews. The first N views in this list are given the values of the first N columns in the from parameter.

        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.

param
context The context where the {@link ExpandableListView} associated with this {@link SimpleCursorTreeAdapter} is running
param
cursor The database cursor
param
groupLayout The resource identifier of a layout file that defines the views for a group. The layout file should include at least those named views defined in groupTo.
param
groupFrom A list of column names that will be used to display the data for a group.
param
groupTo The group views (from the group layouts) that should display column in the "from" parameter. These should all be TextViews or ImageViews. The first N views in this list are given the values of the first N columns in the from parameter.
param
childLayout The resource identifier of a layout file that defines the views for a child. The layout file should include at least those named views defined in childTo.
param
childFrom A list of column names that will be used to display the data for a child.
param
childTo The child views (from the child layouts) that should display column in the "from" parameter. These should all be TextViews or ImageViews. The first N views in this list are given the values of the first N columns in the from parameter.

        super(context, cursor, groupLayout, childLayout);
        init(groupFrom, groupTo, childFrom, childTo);
    
Methods Summary
protected voidbindChildView(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 voidbindGroupView(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 voidbindView(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$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.SimpleCursorTreeAdapter.ViewBinder)

        return mViewBinder;
    
private voidinit(java.lang.String[] groupFromNames, int[] groupTo, java.lang.String[] childFromNames, int[] childTo)

        
        mGroupFromNames = groupFromNames;
        mGroupTo = groupTo;
        
        mChildFromNames = childFromNames;
        mChildTo = childTo;
    
private voidinitFromColumns(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 voidsetViewBinder(android.widget.SimpleCursorTreeAdapter$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;
    
protected voidsetViewImage(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.

param
v ImageView to receive an image
param
value the value retrieved from the cursor

        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 a TextView. Intended to be overridden by Adapters that need to filter strings retrieved from the database.

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

        v.setText(text);