Merge "Change CursorTreeAdapter to close the cursors rather than deactivating them. Fix SimpleCursorTreeAdapter to allow a null cursor as an argument." into gingerbread

This commit is contained in:
Jason Parks
2010-07-08 11:50:40 -07:00
committed by Android (Google) Code Review
2 changed files with 31 additions and 32 deletions

View File

@ -134,14 +134,16 @@ public abstract class CursorTreeAdapter extends BaseExpandableListAdapter implem
/**
* Sets the group Cursor.
*
* @param cursor The Cursor to set for the group.
* @param cursor The Cursor to set for the group. If there is an existing cursor
* it will be closed.
*/
public void setGroupCursor(Cursor cursor) {
mGroupCursorHelper.changeCursor(cursor, false);
}
/**
* Sets the children Cursor for a particular group.
* Sets the children Cursor for a particular group. If there is an existing cursor
* it will be closed.
* <p>
* This is useful when asynchronously querying to prevent blocking the UI.
*
@ -476,7 +478,7 @@ public abstract class CursorTreeAdapter extends BaseExpandableListAdapter implem
mCursor.unregisterContentObserver(mContentObserver);
mCursor.unregisterDataSetObserver(mDataSetObserver);
mCursor.deactivate();
mCursor.close();
mCursor = null;
}

View File

@ -38,6 +38,10 @@ import android.view.View;
* binding can be found, an {@link IllegalStateException} is thrown.
*/
public abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter {
/** The name of the columns that contain the data to display for a group. */
private String[] mGroupFromNames;
/** The indices of columns that contain data to display for a group. */
private int[] mGroupFrom;
/**
@ -46,6 +50,9 @@ public abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter
*/
private int[] mGroupTo;
/** The name of the columns that contain the data to display for a child. */
private String[] mChildFromNames;
/** The indices of columns that contain data to display for a child. */
private int[] mChildFrom;
/**
@ -171,38 +178,12 @@ public abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter
private void init(String[] groupFromNames, int[] groupTo, String[] childFromNames,
int[] childTo) {
mGroupFromNames = groupFromNames;
mGroupTo = groupTo;
mChildFromNames = childFromNames;
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 void initFromColumns(Cursor cursor, String[] fromColumnNames, int[] fromColumns) {
for (int i = fromColumnNames.length - 1; i >= 0; i--) {
fromColumns[i] = cursor.getColumnIndexOrThrow(fromColumnNames[i]);
}
}
private void initGroupFromColumns(String[] groupFromNames) {
mGroupFrom = new int[groupFromNames.length];
initFromColumns(mGroupCursorHelper.getCursor(), groupFromNames, mGroupFrom);
}
private void initChildrenFromColumns(String[] childFromNames, Cursor childCursor) {
mChildFrom = new int[childFromNames.length];
initFromColumns(childCursor, childFromNames, mChildFrom);
}
/**
@ -257,13 +238,29 @@ public abstract class SimpleCursorTreeAdapter extends ResourceCursorTreeAdapter
}
}
private void initFromColumns(Cursor cursor, String[] fromColumnNames, int[] fromColumns) {
for (int i = fromColumnNames.length - 1; i >= 0; i--) {
fromColumns[i] = cursor.getColumnIndexOrThrow(fromColumnNames[i]);
}
}
@Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
if (mChildFrom == null) {
mChildFrom = new int[mChildFromNames.length];
initFromColumns(cursor, mChildFromNames, mChildFrom);
}
bindView(view, context, cursor, mChildFrom, mChildTo);
}
@Override
protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
if (mGroupFrom == null) {
mGroupFrom = new int[mGroupFromNames.length];
initFromColumns(cursor, mGroupFromNames, mGroupFrom);
}
bindView(view, context, cursor, mGroupFrom, mGroupTo);
}