public class

CursorObjectAdapter

extends ObjectAdapter

 java.lang.Object

androidx.leanback.widget.ObjectAdapter

↳androidx.leanback.widget.CursorObjectAdapter

Gradle dependencies

compile group: 'androidx.leanback', name: 'leanback', version: '1.2.0-alpha04'

  • groupId: androidx.leanback
  • artifactId: leanback
  • version: 1.2.0-alpha04

Artifact androidx.leanback:leanback:1.2.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.leanback:leanback com.android.support:leanback-v17

Androidx class mapping:

androidx.leanback.widget.CursorObjectAdapter android.support.v17.leanback.widget.CursorObjectAdapter

Overview

An ObjectAdapter implemented with a .

Summary

Fields
from ObjectAdapterNO_ID
Constructors
publicCursorObjectAdapter()

Constructs an adapter.

publicCursorObjectAdapter(Presenter presenter)

Constructs an adapter that uses the given Presenter for all items.

publicCursorObjectAdapter(PresenterSelector presenterSelector)

Constructs an adapter with the given PresenterSelector.

Methods
public voidchangeCursor(Cursor cursor)

Changes the underlying cursor to a new cursor.

public voidclose()

Closes this adapter, closing the backing as well.

public abstract java.lang.Objectget(int position)

Returns the item for the given position.

public final CursorgetCursor()

Returns the backing the adapter.

public final CursorMappergetMapper()

Returns the CursorMapper used to convert rows into Objects.

protected final voidinvalidateCache(int index)

Removes an item from the cache.

protected final voidinvalidateCache(int index, int count)

Removes count items starting at index.

public booleanisClosed()

Returns true if the adapter, and hence the backing , is closed; false otherwise.

public booleanisImmediateNotifySupported()

Returns true if the adapter pairs each underlying data change with a call to notify and false otherwise.

protected voidonCursorChanged()

Called whenever the cursor changes.

protected voidonMapperChanged()

Called when CursorObjectAdapter.setMapper(CursorMapper) is called and a different mapper is provided.

public final voidsetMapper(CursorMapper mapper)

Sets the CursorMapper used to convert rows into Objects.

public abstract intsize()

Returns the number of items in the adapter.

public CursorswapCursor(Cursor cursor)

Swap in a new Cursor, returning the old Cursor.

from ObjectAdaptergetId, getPresenter, getPresenterSelector, hasObserver, hasStableIds, notifyChanged, notifyItemMoved, notifyItemRangeChanged, notifyItemRangeChanged, notifyItemRangeInserted, notifyItemRangeRemoved, onHasStableIdsChanged, onPresenterSelectorChanged, registerObserver, setHasStableIds, setPresenterSelector, unregisterAllObservers, unregisterObserver
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public CursorObjectAdapter(PresenterSelector presenterSelector)

Constructs an adapter with the given PresenterSelector.

public CursorObjectAdapter(Presenter presenter)

Constructs an adapter that uses the given Presenter for all items.

public CursorObjectAdapter()

Constructs an adapter.

Methods

public void changeCursor(Cursor cursor)

Changes 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.

Parameters:

cursor: The new cursor to be used.

public Cursor swapCursor(Cursor cursor)

Swap in a new Cursor, returning the old Cursor. Unlike changeCursor(Cursor), the returned old Cursor is not closed.

Parameters:

cursor: The new cursor to be used.

protected void onCursorChanged()

Called whenever the cursor changes.

public final Cursor getCursor()

Returns the backing the adapter.

public final void setMapper(CursorMapper mapper)

Sets the CursorMapper used to convert rows into Objects.

protected void onMapperChanged()

Called when CursorObjectAdapter.setMapper(CursorMapper) is called and a different mapper is provided.

public final CursorMapper getMapper()

Returns the CursorMapper used to convert rows into Objects.

public abstract int size()

Returns the number of items in the adapter.

public abstract java.lang.Object get(int position)

Returns the item for the given position.

public void close()

Closes this adapter, closing the backing as well.

public boolean isClosed()

Returns true if the adapter, and hence the backing , is closed; false otherwise.

protected final void invalidateCache(int index)

Removes an item from the cache. This will force the item to be re-read from the data source the next time CursorObjectAdapter.get(int) is called.

protected final void invalidateCache(int index, int count)

Removes count items starting at index.

public boolean isImmediateNotifySupported()

Returns true if the adapter pairs each underlying data change with a call to notify and false otherwise.

Source

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package androidx.leanback.widget;

import android.database.Cursor;
import android.util.LruCache;

import androidx.annotation.Nullable;
import androidx.leanback.database.CursorMapper;

/**
 * An {@link ObjectAdapter} implemented with a {@link Cursor}.
 */
public class CursorObjectAdapter extends ObjectAdapter {
    private static final int CACHE_SIZE = 100;
    private Cursor mCursor;
    private CursorMapper mMapper;
    private final LruCache<Integer, Object> mItemCache = new LruCache<Integer, Object>(CACHE_SIZE);

    /**
     * Constructs an adapter with the given {@link PresenterSelector}.
     */
    public CursorObjectAdapter(PresenterSelector presenterSelector) {
        super(presenterSelector);
    }

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

    /**
     * Constructs an adapter.
     */
    public CursorObjectAdapter() {
        super();
    }

    /**
     * Changes 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.
     */
    public void changeCursor(Cursor cursor) {
        if (cursor == mCursor) {
            return;
        }
        if (mCursor != null) {
            mCursor.close();
        }
        mCursor = cursor;
        mItemCache.trimToSize(0);
        onCursorChanged();
    }

    /**
     * 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.
     */
    public Cursor swapCursor(Cursor cursor) {
        if (cursor == mCursor) {
            return mCursor;
        }
        Cursor oldCursor = mCursor;
        mCursor = cursor;
        mItemCache.trimToSize(0);
        onCursorChanged();
        return oldCursor;
    }

    /**
     * Called whenever the cursor changes.
     */
    protected void onCursorChanged() {
        notifyChanged();
    }

    /**
     * Returns the {@link Cursor} backing the adapter.
     */
     public final Cursor getCursor() {
        return mCursor;
    }

    /**
     * Sets the {@link CursorMapper} used to convert {@link Cursor} rows into
     * Objects.
     */
    public final void setMapper(CursorMapper mapper) {
        boolean changed = mMapper != mapper;
        mMapper = mapper;

        if (changed) {
            onMapperChanged();
        }
    }

    /**
     * Called when {@link #setMapper(CursorMapper)} is called and a different
     * mapper is provided.
     */
    protected void onMapperChanged() {
    }

    /**
     * Returns the {@link CursorMapper} used to convert {@link Cursor} rows into
     * Objects.
     */
    public final CursorMapper getMapper() {
        return mMapper;
    }

    @Override
    public int size() {
        if (mCursor == null) {
            return 0;
        }
        return mCursor.getCount();
    }

    @Override
    public @Nullable Object get(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;
    }

    /**
     * Closes this adapter, closing the backing {@link Cursor} as well.
     */
    public void close() {
        if (mCursor != null) {
            mCursor.close();
            mCursor = null;
        }
    }

    /**
     * Returns true if the adapter, and hence the backing {@link Cursor}, is closed; false
     * otherwise.
     */
    public boolean isClosed() {
        return mCursor == null || mCursor.isClosed();
    }

    /**
     * Removes 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.
     */
    protected final void invalidateCache(int index) {
        mItemCache.remove(index);
    }

    /**
     * Removes {@code count} items starting at {@code index}.
     */
    protected final void invalidateCache(int index, int count) {
        for (int limit = count + index; index < limit; index++) {
            invalidateCache(index);
        }
    }

    @Override
    public boolean isImmediateNotifySupported() {
        return true;
    }
}