FileDocCategorySizeDatePackage
SimpleCursorTreeAdapter.javaAPI DocAndroid 1.5 API11621Wed May 06 22:41:56 BST 2009android.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. TextViews bind the values to their text property (see {@link TextView#setText(CharSequence)}). ImageViews bind the values to their image's Uri property (see {@link ImageView#setImageURI(android.net.Uri)}).

Fields Summary
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 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.
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)

        bindView(view, context, cursor, mChildFrom, mChildTo);
    
protected voidbindGroupView(android.view.View view, android.content.Context context, android.database.Cursor cursor, boolean isExpanded)

        bindView(view, context, cursor, mGroupFrom, mGroupTo);
    
private voidbindView(android.view.View view, android.content.Context context, android.database.Cursor cursor, int[] from, int[] to)

        for (int i = 0; i < to.length; i++) {
            View v = view.findViewById(to[i]);
            if (v != null) {
                String text = cursor.getString(from[i]);
                if (text == null) {
                    text = "";
                }
                if (v instanceof TextView) {
                    ((TextView) v).setText(text);
                } else if (v instanceof ImageView) {
                    setViewImage((ImageView) v, text);
                } else {
                    throw new IllegalStateException("SimpleCursorAdapter can bind values only to" +
                            " TextView and ImageView!");
                }
            }
        }
    
private voidinit(java.lang.String[] groupFromNames, int[] groupTo, java.lang.String[] childFromNames, int[] childTo)

        mGroupTo = groupTo;
        
        mChildTo = childTo;
        
        // Get the group cursor column indices, the child cursor column indices will come
        // when needed
        initGroupFromColumns(groupFromNames);
        
        // Get a temporary child cursor to init the column indices
        if (getGroupCount() > 0) {
            MyCursorHelper tmpCursorHelper = getChildrenCursorHelper(0, true);
            if (tmpCursorHelper != null) {
                initChildrenFromColumns(childFromNames, tmpCursorHelper.getCursor());
                deactivateChildrenCursorHelper(0);
            }
        }
    
private voidinitChildrenFromColumns(java.lang.String[] childFromNames, android.database.Cursor childCursor)

        mChildFrom = new int[childFromNames.length];
        initFromColumns(childCursor, childFromNames, mChildFrom);
    
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]);
        }
    
private voidinitGroupFromColumns(java.lang.String[] groupFromNames)

        mGroupFrom = new int[groupFromNames.length];
        initFromColumns(mGroupCursorHelper.getCursor(), groupFromNames, mGroupFrom);
    
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));
        }