FileDocCategorySizeDatePackage
CursorTreeAdapter.javaAPI DocAndroid 5.1 API18364Thu Mar 12 22:22:10 GMT 2015android.widget

CursorTreeAdapter

public abstract class CursorTreeAdapter extends BaseExpandableListAdapter implements Filterable, CursorFilter.CursorFilterClient
An adapter that exposes data from a series of {@link Cursor}s to an {@link ExpandableListView} widget. The top-level {@link Cursor} (that is given in the constructor) exposes the groups, while subsequent {@link Cursor}s returned from {@link #getChildrenCursor(Cursor)} expose children within a particular group. The Cursors must include a column named "_id" or this class will not work.

Fields Summary
private android.content.Context
mContext
private android.os.Handler
mHandler
private boolean
mAutoRequery
MyCursorHelper
mGroupCursorHelper
The cursor helper that is used to get the groups
android.util.SparseArray
mChildrenCursorHelpers
The map of a group position to the group's children cursor helper (the cursor helper that is used to get the children for that group)
CursorFilter
mCursorFilter
FilterQueryProvider
mFilterQueryProvider
Constructors Summary
public CursorTreeAdapter(android.database.Cursor cursor, android.content.Context context)
Constructor. The adapter will call {@link Cursor#requery()} on the cursor whenever it changes so that the most recent data is always displayed.

param
cursor The cursor from which to get the data for the groups.

        init(cursor, context, true);
    
public CursorTreeAdapter(android.database.Cursor cursor, android.content.Context context, boolean autoRequery)
Constructor.

param
cursor The cursor from which to get the data for the groups.
param
context The context
param
autoRequery If true the adapter will call {@link Cursor#requery()} on the cursor whenever it changes so the most recent data is always displayed.

        init(cursor, context, autoRequery);
    
Methods Summary
protected abstract voidbindChildView(android.view.View view, android.content.Context context, android.database.Cursor cursor, boolean isLastChild)
Bind an existing view to the child data pointed to by cursor

param
view Existing view, returned earlier by newChildView
param
context Interface to application's global information
param
cursor The cursor from which to get the data. The cursor is already moved to the correct position.
param
isLastChild Whether the child is the last child within its group.

protected abstract voidbindGroupView(android.view.View view, android.content.Context context, android.database.Cursor cursor, boolean isExpanded)
Bind an existing view to the group data pointed to by cursor.

param
view Existing view, returned earlier by newGroupView.
param
context Interface to application's global information
param
cursor The cursor from which to get the data. The cursor is already moved to the correct position.
param
isExpanded Whether the group is expanded.

public voidchangeCursor(android.database.Cursor cursor)

see
CursorAdapter#changeCursor(Cursor)

        mGroupCursorHelper.changeCursor(cursor, true);
    
public java.lang.StringconvertToString(android.database.Cursor cursor)

see
CursorAdapter#convertToString(Cursor)

        return cursor == null ? "" : cursor.toString();
    
synchronized voiddeactivateChildrenCursorHelper(int groupPosition)
Deactivates the Cursor and removes the helper from cache.

param
groupPosition The group whose children Cursor and helper should be deactivated.

        MyCursorHelper cursorHelper = getChildrenCursorHelper(groupPosition, true);
        mChildrenCursorHelpers.remove(groupPosition);
        cursorHelper.deactivate();
    
public android.database.CursorgetChild(int groupPosition, int childPosition)

        // Return this group's children Cursor pointing to the particular child
        return getChildrenCursorHelper(groupPosition, true).moveTo(childPosition);
    
public longgetChildId(int groupPosition, int childPosition)

        return getChildrenCursorHelper(groupPosition, true).getId(childPosition);
    
public android.view.ViewgetChildView(int groupPosition, int childPosition, boolean isLastChild, android.view.View convertView, android.view.ViewGroup parent)

        MyCursorHelper cursorHelper = getChildrenCursorHelper(groupPosition, true);
        
        Cursor cursor = cursorHelper.moveTo(childPosition);
        if (cursor == null) {
            throw new IllegalStateException("this should only be called when the cursor is valid");
        }
        
        View v;
        if (convertView == null) {
            v = newChildView(mContext, cursor, isLastChild, parent);
        } else {
            v = convertView;
        }
        bindChildView(v, mContext, cursor, isLastChild);
        return v;
    
public intgetChildrenCount(int groupPosition)

        MyCursorHelper helper = getChildrenCursorHelper(groupPosition, true);
        return (mGroupCursorHelper.isValid() && helper != null) ? helper.getCount() : 0;
    
protected abstract android.database.CursorgetChildrenCursor(android.database.Cursor groupCursor)
Gets the Cursor for the children at the given group. Subclasses must implement this method to return the children data for a particular group.

If you want to asynchronously query a provider to prevent blocking the UI, it is possible to return null and at a later time call {@link #setChildrenCursor(int, Cursor)}.

It is your responsibility to manage this Cursor through the Activity lifecycle. It is a good idea to use {@link Activity#managedQuery} which will handle this for you. In some situations, the adapter will deactivate the Cursor on its own, but this will not always be the case, so please ensure the Cursor is properly managed.

param
groupCursor The cursor pointing to the group whose children cursor should be returned
return
The cursor for the children of a particular group, or null.

synchronized android.widget.CursorTreeAdapter$MyCursorHelpergetChildrenCursorHelper(int groupPosition, boolean requestCursor)
Gets the cursor helper for the children in the given group.

param
groupPosition The group whose children will be returned
param
requestCursor Whether to request a Cursor via {@link #getChildrenCursor(Cursor)} (true), or to assume a call to {@link #setChildrenCursor(int, Cursor)} will happen shortly (false).
return
The cursor helper for the children of the given group

        MyCursorHelper cursorHelper = mChildrenCursorHelpers.get(groupPosition);
        
        if (cursorHelper == null) {
            if (mGroupCursorHelper.moveTo(groupPosition) == null) return null;
            
            final Cursor cursor = getChildrenCursor(mGroupCursorHelper.getCursor());
            cursorHelper = new MyCursorHelper(cursor);
            mChildrenCursorHelpers.put(groupPosition, cursorHelper);
        }
        
        return cursorHelper;
    
public android.database.CursorgetCursor()

see
CursorAdapter#getCursor()

        return mGroupCursorHelper.getCursor();
    
public FiltergetFilter()

        if (mCursorFilter == null) {
            mCursorFilter = new CursorFilter(this);
        }
        return mCursorFilter;
    
public FilterQueryProvidergetFilterQueryProvider()

see
CursorAdapter#getFilterQueryProvider()

        return mFilterQueryProvider;
    
public android.database.CursorgetGroup(int groupPosition)

        // Return the group Cursor pointing to the given group
        return mGroupCursorHelper.moveTo(groupPosition);
    
public intgetGroupCount()

        return mGroupCursorHelper.getCount();
    
public longgetGroupId(int groupPosition)

        return mGroupCursorHelper.getId(groupPosition);
    
public android.view.ViewgetGroupView(int groupPosition, boolean isExpanded, android.view.View convertView, android.view.ViewGroup parent)

        Cursor cursor = mGroupCursorHelper.moveTo(groupPosition);
        if (cursor == null) {
            throw new IllegalStateException("this should only be called when the cursor is valid");
        }
        
        View v;
        if (convertView == null) {
            v = newGroupView(mContext, cursor, isExpanded, parent);
        } else {
            v = convertView;
        }
        bindGroupView(v, mContext, cursor, isExpanded);
        return v;
    
public booleanhasStableIds()

        return true;
    
private voidinit(android.database.Cursor cursor, android.content.Context context, boolean autoRequery)

        mContext = context;
        mHandler = new Handler();
        mAutoRequery = autoRequery;
        
        mGroupCursorHelper = new MyCursorHelper(cursor);
        mChildrenCursorHelpers = new SparseArray<MyCursorHelper>();
    
public booleanisChildSelectable(int groupPosition, int childPosition)

        return true;
    
protected abstract android.view.ViewnewChildView(android.content.Context context, android.database.Cursor cursor, boolean isLastChild, android.view.ViewGroup parent)
Makes a new child view to hold the data pointed to by cursor.

param
context Interface to application's global information
param
cursor The cursor from which to get the data. The cursor is already moved to the correct position.
param
isLastChild Whether the child is the last child within its group.
param
parent The parent to which the new view is attached to
return
the newly created view.

protected abstract android.view.ViewnewGroupView(android.content.Context context, android.database.Cursor cursor, boolean isExpanded, android.view.ViewGroup parent)
Makes a new group view to hold the group data pointed to by cursor.

param
context Interface to application's global information
param
cursor The group cursor from which to get the data. The cursor is already moved to the correct position.
param
isExpanded Whether the group is expanded.
param
parent The parent to which the new view is attached to
return
The newly created view.

public voidnotifyDataSetChanged()

        notifyDataSetChanged(true);
    
public voidnotifyDataSetChanged(boolean releaseCursors)
Notifies a data set change, but with the option of not releasing any cached cursors.

param
releaseCursors Whether to release and deactivate any cached cursors.

        
        if (releaseCursors) {
            releaseCursorHelpers();
        }
        
        super.notifyDataSetChanged();
    
public voidnotifyDataSetInvalidated()

        releaseCursorHelpers();
        super.notifyDataSetInvalidated();
    
public voidonGroupCollapsed(int groupPosition)

        deactivateChildrenCursorHelper(groupPosition);
    
private synchronized voidreleaseCursorHelpers()

        for (int pos = mChildrenCursorHelpers.size() - 1; pos >= 0; pos--) {
            mChildrenCursorHelpers.valueAt(pos).deactivate();
        }
        
        mChildrenCursorHelpers.clear();
    
public android.database.CursorrunQueryOnBackgroundThread(java.lang.CharSequence constraint)

see
CursorAdapter#runQueryOnBackgroundThread(CharSequence)

        if (mFilterQueryProvider != null) {
            return mFilterQueryProvider.runQuery(constraint);
        }

        return mGroupCursorHelper.getCursor();
    
public voidsetChildrenCursor(int groupPosition, android.database.Cursor childrenCursor)
Sets the children Cursor for a particular group. If there is an existing cursor it will be closed.

This is useful when asynchronously querying to prevent blocking the UI.

param
groupPosition The group whose children are being set via this Cursor.
param
childrenCursor The Cursor that contains the children of the group.

        
        /*
         * Don't request a cursor from the subclass, instead we will be setting
         * the cursor ourselves.
         */
        MyCursorHelper childrenCursorHelper = getChildrenCursorHelper(groupPosition, false);

        /*
         * Don't release any cursor since we know exactly what data is changing
         * (this cursor, which is still valid).
         */
        childrenCursorHelper.changeCursor(childrenCursor, false);
    
public voidsetFilterQueryProvider(FilterQueryProvider filterQueryProvider)

see
CursorAdapter#setFilterQueryProvider(FilterQueryProvider)

        mFilterQueryProvider = filterQueryProvider;
    
public voidsetGroupCursor(android.database.Cursor cursor)
Sets the group Cursor.

param
cursor The Cursor to set for the group. If there is an existing cursor it will be closed.

        mGroupCursorHelper.changeCursor(cursor, false);