FileDocCategorySizeDatePackage
BaseCursorPagerAdapter.javaAPI DocAndroid 5.1 API8105Thu Mar 12 22:22:52 GMT 2015com.android.ex.photo.adapters

BaseCursorPagerAdapter

public abstract class BaseCursorPagerAdapter extends BaseFragmentPagerAdapter
Page adapter for use with an BaseCursorLoader. Unlike other cursor adapters, this has no observers for automatic refresh. Instead, it depends upon external mechanisms to provide the update signal.

Fields Summary
private static final String
TAG
protected android.content.Context
mContext
protected android.database.Cursor
mCursor
protected int
mRowIDColumn
protected android.util.SparseIntArray
mItemPosition
Mapping of row ID to cursor position
protected final HashMap
mObjectRowMap
Mapping of instantiated object to row ID
Constructors Summary
public BaseCursorPagerAdapter(android.content.Context context, android.support.v4.app.FragmentManager fm, android.database.Cursor c)
Constructor that always enables auto-requery.

param
c The cursor from which to get the data.
param
context The context


                            
           
        super(fm);
        init(context, c);
    
Methods Summary
public java.lang.CharSequenceconvertToString(android.database.Cursor cursor)
Converts the cursor into a CharSequence. Subclasses should override this method to convert their results. The default implementation returns an empty String for null values or the default String representation of the value.

param
cursor the cursor to convert to a CharSequence
return
a CharSequence representing the value

        return cursor == null ? "" : cursor.toString();
    
public voiddestroyItem(android.view.View container, int position, java.lang.Object object)

        mObjectRowMap.remove(object);

        super.destroyItem(container, position, object);
    
public intgetCount()

        if (mCursor != null) {
            return mCursor.getCount();
        } else {
            return 0;
        }
    
public android.database.CursorgetCursor()
Returns the cursor.

        return mCursor;
    
public java.lang.ObjectgetDataItem(int position)
Returns the data item associated with the specified position in the data set.

        if (mCursor != null && moveCursorTo(position)) {
            return mCursor;
        } else {
            return null;
        }
    
public abstract android.support.v4.app.FragmentgetItem(android.content.Context context, android.database.Cursor cursor, int position)
Makes a fragment for the data pointed to by the 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.
return
the newly created fragment.

public android.support.v4.app.FragmentgetItem(int position)

        if (mCursor != null && moveCursorTo(position)) {
            return getItem(mContext, mCursor, position);
        }
        return null;
    
public longgetItemId(int position)
Returns the row id associated with the specified position in the list.

        if (mCursor != null && moveCursorTo(position)) {
            return mCursor.getString(mRowIDColumn).hashCode();
        } else {
            return 0;
        }
    
public intgetItemPosition(java.lang.Object object)

        final Integer rowId = mObjectRowMap.get(object);
        if (rowId == null || mItemPosition == null) {
            return POSITION_NONE;
        }

        final int position = mItemPosition.get(rowId, POSITION_NONE);
        return position;
    
private voidinit(android.content.Context context, android.database.Cursor c)
Initialize the adapter.

        boolean cursorPresent = c != null;
        mCursor = c;
        mContext = context;
        mRowIDColumn = cursorPresent
                ? mCursor.getColumnIndex(PhotoContract.PhotoViewColumns.URI) : -1;
    
public java.lang.ObjectinstantiateItem(android.view.View container, int position)

        if (mCursor == null) {
            throw new IllegalStateException("this should only be called when the cursor is valid");
        }

        final Integer rowId;
        if (moveCursorTo(position)) {
            rowId = mCursor.getString(mRowIDColumn).hashCode();
        } else {
            rowId = null;
        }

        // Create the fragment and bind cursor data
        final Object obj = super.instantiateItem(container, position);
        if (obj != null) {
            mObjectRowMap.put(obj, rowId);
        }
        return obj;
    
public booleanisDataValid()

return
true if data is valid

        return mCursor != null;
    
protected java.lang.StringmakeFragmentName(int viewId, int index)

        if (moveCursorTo(index)) {
            return "android:pager:" + viewId + ":" + mCursor.getString(mRowIDColumn).hashCode();
        } else {
            return super.makeFragmentName(viewId, index);
        }
    
private booleanmoveCursorTo(int position)
Moves the cursor to the given position

return
{@code true} if the cursor's position was set. Otherwise, {@code false}.

        if (mCursor != null && !mCursor.isClosed()) {
            return mCursor.moveToPosition(position);
        }
        return false;
    
private voidsetItemPosition()
Sets the {@link #mItemPosition} instance variable with the current mapping of row id to cursor position.

        if (mCursor == null || mCursor.isClosed()) {
            mItemPosition = null;
            return;
        }

        SparseIntArray itemPosition = new SparseIntArray(mCursor.getCount());

        mCursor.moveToPosition(-1);
        while (mCursor.moveToNext()) {
            final int rowId = mCursor.getString(mRowIDColumn).hashCode();
            final int position = mCursor.getPosition();

            itemPosition.append(rowId, position);
        }
        mItemPosition = itemPosition;
    
public android.database.CursorswapCursor(android.database.Cursor newCursor)
Swap in a new Cursor, returning the old Cursor.

param
newCursor The new cursor to be used.
return
Returns the previously set Cursor, or null if there was not one. If the given new Cursor is the same instance is the previously set Cursor, null is also returned.

        if (Log.isLoggable(TAG, Log.VERBOSE)) {
            Log.v(TAG, "swapCursor old=" + (mCursor == null ? -1 : mCursor.getCount()) +
                    "; new=" + (newCursor == null ? -1 : newCursor.getCount()));
        }

        if (newCursor == mCursor) {
            return null;
        }
        Cursor oldCursor = mCursor;
        mCursor = newCursor;
        if (newCursor != null) {
            mRowIDColumn = newCursor.getColumnIndex(PhotoContract.PhotoViewColumns.URI);
        } else {
            mRowIDColumn = -1;
        }

        setItemPosition();
        notifyDataSetChanged();     // notify the observers about the new cursor
        return oldCursor;