FileDocCategorySizeDatePackage
CursorObjectAdapter.javaAPI DocAndroid 5.1 API5011Thu Mar 12 22:22:56 GMT 2015android.support.v17.leanback.widget

CursorObjectAdapter

public class CursorObjectAdapter extends ObjectAdapter
An ObjectAdapter implemented with a {@link Cursor}.

Fields Summary
private static final int
CACHE_SIZE
private android.database.Cursor
mCursor
private android.support.v17.leanback.database.CursorMapper
mMapper
private final android.util.LruCache
mItemCache
Constructors Summary
public CursorObjectAdapter(PresenterSelector presenterSelector)
Construct an adapter with the given {@link PresenterSelector}.


                 
       
        super(presenterSelector);
    
public CursorObjectAdapter(Presenter presenter)
Construct an adapter that uses the given {@link Presenter} for all items.

        super(presenter);
    
public CursorObjectAdapter()
Construct an adapter.

        super();
    
Methods Summary
public voidchangeCursor(android.database.Cursor cursor)
Change the underlying cursor to a new cursor. If there is an existing cursor it will be closed if it is different than the new cursor.

param
cursor The new cursor to be used.

        if (cursor == mCursor) {
            return;
        }
        if (mCursor != null) {
            mCursor.close();
        }
        mCursor = cursor;
        mItemCache.trimToSize(0);
        onCursorChanged();
    
public voidclose()
Closes this adapter, closing the backing {@link Cursor} as well.

        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
        }
    
public java.lang.Objectget(int index)

        if (mCursor == null) {
            return null;
        }
        if (!mCursor.moveToPosition(index)) {
            throw new ArrayIndexOutOfBoundsException();
        }
        Object item = mItemCache.get(index);
        if (item != null) {
            return item;
        }
        item = mMapper.convert(mCursor);
        mItemCache.put(index, item);
        return item;
    
public final android.database.CursorgetCursor()
Gets the {@link Cursor} backing the adapter.

        return mCursor;
    
public final android.support.v17.leanback.database.CursorMappergetMapper()
Gets the {@link CursorMapper} used to convert {@link Cursor} rows into Objects.

        return mMapper;
    
protected final voidinvalidateCache(int index)
Remove an item from the cache. This will force the item to be re-read from the data source the next time (@link #get(int)} is called.

        mItemCache.remove(index);
    
protected final voidinvalidateCache(int index, int count)
Remove {@code count} items starting at {@code index}.

        for (int limit = count + index; index < limit; index++) {
            invalidateCache(index);
        }
    
public booleanisClosed()
Checks whether the adapter, and hence the backing {@link Cursor}, is closed.

        return mCursor == null || mCursor.isClosed();
    
protected voidonCursorChanged()
Called whenever the cursor changes.

        notifyChanged();
    
protected voidonMapperChanged()
Called when {@link #setMapper(CursorMapper)} is called and a different mapper is provided.

    
public final voidsetMapper(android.support.v17.leanback.database.CursorMapper mapper)
Sets the {@link CursorMapper} used to convert {@link Cursor} rows into Objects.

        boolean changed = mMapper != mapper;
        mMapper = mapper;

        if (changed) {
            onMapperChanged();
        }
    
public intsize()

        if (mCursor == null) {
            return 0;
        }
        return mCursor.getCount();
    
public android.database.CursorswapCursor(android.database.Cursor cursor)
Swap in a new Cursor, returning the old Cursor. Unlike changeCursor(Cursor), the returned old Cursor is not closed.

param
cursor The new cursor to be used.

        if (cursor == mCursor) {
            return mCursor;
        }
        Cursor oldCursor = mCursor;
        mCursor = cursor;
        mItemCache.trimToSize(0);
        onCursorChanged();
        return oldCursor;