# ObjectAdapter

# ObjectAdapter

Artifact:[androidx.leanback:leanback](https://developer.android.com/jetpack/androidx/releases/leanback)  
[View Source](https://cs.android.com/search?q=file:androidx/leanback/widget/ObjectAdapter.java+class:androidx.leanback.widget.ObjectAdapter)  
Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/leanback/widget/ObjectAdapter "View this page in Java")

<br />

```
abstract class ObjectAdapter
```

<br />

Known direct subclasses  
[ArrayObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ArrayObjectAdapter),[CursorObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/CursorObjectAdapter),[PagingDataAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/paging/PagingDataAdapter),[SparseArrayObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/SparseArrayObjectAdapter)  

|------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ArrayObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ArrayObjectAdapter)             | An[ObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter)implemented with an[ArrayList](https://developer.android.com/reference/java/util/ArrayList.html).                                 |
| [CursorObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/CursorObjectAdapter)           | An[ObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter)implemented with a[Cursor](https://developer.android.com/reference/android/database/Cursor.html).                                 |
| [PagingDataAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/paging/PagingDataAdapter)               | An[ObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter)implemented with an[AsyncPagingDataDiffer](https://developer.android.com/reference/kotlin/androidx/paging/AsyncPagingDataDiffer). |
| [SparseArrayObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/SparseArrayObjectAdapter) | An[ObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter)implemented with a[android.util.SparseArray](https://developer.android.com/reference/android/util/SparseArray.html).              |

*** ** * ** ***

Base class adapter to be used in leanback activities. Provides access to a data model and is decoupled from the presentation of the items via[PresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/PresenterSelector).

## Summary

|                                                                                                         ### Nested types                                                                                                          |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `abstract class `[ObjectAdapter.DataObserver](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter.DataObserver) A DataObserver can be notified when an ObjectAdapter's underlying data changes. |

|                                    ### Constants                                    |
|-------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [NO_ID](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#NO_ID())` = -1` Indicates that an id has not been set. |

|                                                                                                                                                                                                                   ### Public constructors                                                                                                                                                                                                                   |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#ObjectAdapter())`()` Constructs an adapter.                                                                                                                                                                                                                                                                                                           |
| [ObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#ObjectAdapter(androidx.leanback.widget.Presenter))`(presenter: `[Presenter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/Presenter)`)` Constructs an adapter that uses the given[Presenter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/Presenter)for all items.                               |
| [ObjectAdapter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#ObjectAdapter(androidx.leanback.widget.PresenterSelector))`(presenterSelector: `[PresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/PresenterSelector)`)` Constructs an adapter with the given[PresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/PresenterSelector). |

|                                              ### Public functions                                              |
|----------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `abstract `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?`                      | [get](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#get(int))`(position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the item for the given position.                                                                                                                                                                                                                                                    |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                  | [getId](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#getId(int))`(position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the id for the given position.                                                                                                                                                                                                                                                  |
| [Presenter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/Presenter)`?`              | [getPresenter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#getPresenter(java.lang.Object))`(item: `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`)` Returns the[Presenter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/Presenter)for the given item from the adapter.                                                                                                                    |
| [PresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/PresenterSelector) | [getPresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#getPresenterSelector())`()` Returns the presenter selector for this ObjectAdapter.                                                                                                                                                                                                                                                                                              |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                            | [hasStableIds](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#hasStableIds())`()` Returns true if the item ids are stable across changes to the underlying data.                                                                                                                                                                                                                                                                                      |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                            | [isImmediateNotifySupported](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#isImmediateNotifySupported())`()` Returns true if the adapter pairs each underlying data change with a call to notify and false otherwise.                                                                                                                                                                                                                                |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                  | [notifyItemRangeChanged](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#notifyItemRangeChanged(int,int))`(positionStart: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, itemCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Notifies UI that some items has changed.                                                                                                           |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                  | [notifyItemRangeChanged](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#notifyItemRangeChanged(int,int,java.lang.Object))`(positionStart: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, itemCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, payload: `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?)` Notifies UI that some items has changed. |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                  | [registerObserver](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#registerObserver(androidx.leanback.widget.ObjectAdapter.DataObserver))`(observer: `[ObjectAdapter.DataObserver](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter.DataObserver)`)` Registers a DataObserver for data change notifications.                                                                                                      |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                  | [setHasStableIds](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#setHasStableIds(boolean))`(hasStableIds: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`)` Sets whether the item ids are stable across changes to the underlying data.                                                                                                                                                                         |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                  | [setPresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#setPresenterSelector(androidx.leanback.widget.PresenterSelector))`(presenterSelector: `[PresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/PresenterSelector)`)` Sets the presenter selector.                                                                                                                                           |
| `abstract `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                         | [size](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#size())`()` Returns the number of items in the adapter.                                                                                                                                                                                                                                                                                                                                         |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                  | [unregisterAllObservers](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#unregisterAllObservers())`()` Unregisters all DataObservers for this ObjectAdapter.                                                                                                                                                                                                                                                                                           |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                  | [unregisterObserver](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#unregisterObserver(androidx.leanback.widget.ObjectAdapter.DataObserver))`(observer: `[ObjectAdapter.DataObserver](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter.DataObserver)`)` Unregisters a DataObserver for data change notifications.                                                                                                |

|                            ### Protected functions                            |
|-------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [notifyChanged](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#notifyChanged())`()` Notifies UI that the underlying data has changed.                                                                                                                                                                                                                                     |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [notifyItemMoved](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#notifyItemMoved(int,int))`(fromPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, toPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Notifies UI that item at fromPosition has been moved to toPosition.                  |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [notifyItemRangeInserted](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#notifyItemRangeInserted(int,int))`(positionStart: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, itemCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Notifies UI that new items has been inserted.                        |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [notifyItemRangeRemoved](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#notifyItemRangeRemoved(int,int))`(positionStart: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, itemCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Notifies UI that some items that has been removed.                     |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onHasStableIdsChanged](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#onHasStableIdsChanged())`()` Called when[setHasStableIds](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#setHasStableIds(boolean))is called and the status of stable ids has changed.                                                                       |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [onPresenterSelectorChanged](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#onPresenterSelectorChanged())`()` Called when[setPresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#setPresenterSelector(androidx.leanback.widget.PresenterSelector))is called and the PresenterSelector differs from the previous one. |

## Constants

### NO_ID

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
const val NO_ID = -1: Int
```

Indicates that an id has not been set.  

## Public constructors

### ObjectAdapter

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
ObjectAdapter()
```

Constructs an adapter.  

### ObjectAdapter

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
ObjectAdapter(presenter: Presenter)
```

Constructs an adapter that uses the given[Presenter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/Presenter)for all items.  

### ObjectAdapter

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
ObjectAdapter(presenterSelector: PresenterSelector)
```

Constructs an adapter with the given[PresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/PresenterSelector).  

## Public functions

### get

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
abstract fun get(position: Int): Any?
```

Returns the item for the given position.  

### getId

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun getId(position: Int): Long
```

Returns the id for the given position.  

### getPresenter

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun getPresenter(item: Any): Presenter?
```

Returns the[Presenter](https://developer.android.com/reference/kotlin/androidx/leanback/widget/Presenter)for the given item from the adapter.  

### getPresenterSelector

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun getPresenterSelector(): PresenterSelector
```

Returns the presenter selector for this ObjectAdapter.  

### hasStableIds

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun hasStableIds(): Boolean
```

Returns true if the item ids are stable across changes to the underlying data. When this is true, clients of the ObjectAdapter can use[getId](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#getId(int))to correlate Objects across changes.  

### isImmediateNotifySupported

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun isImmediateNotifySupported(): Boolean
```

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

### notifyItemRangeChanged

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun notifyItemRangeChanged(positionStart: Int, itemCount: Int): Unit
```

Notifies UI that some items has changed.  

|                                          Parameters                                          |
|----------------------------------------------------------------------------------------------|-----------------------------------------|
| `positionStart: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Starting position of the changed items. |
| `itemCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)     | Total number of items that changed.     |

### notifyItemRangeChanged

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun notifyItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?): Unit
```

Notifies UI that some items has changed.  

|                                          Parameters                                          |
|----------------------------------------------------------------------------------------------|-----------------------------------------------------------|
| `positionStart: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Starting position of the changed items.                   |
| `itemCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)     | Total number of items that changed.                       |
| `payload: `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?`    | Optional parameter, use null to identify a "full" update. |

### registerObserver

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun registerObserver(observer: ObjectAdapter.DataObserver): Unit
```

Registers a DataObserver for data change notifications.  

### setHasStableIds

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun setHasStableIds(hasStableIds: Boolean): Unit
```

Sets whether the item ids are stable across changes to the underlying data.  

### setPresenterSelector

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun setPresenterSelector(presenterSelector: PresenterSelector): Unit
```

Sets the presenter selector. May not be null.  

### size

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
abstract fun size(): Int
```

Returns the number of items in the adapter.  

### unregisterAllObservers

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun unregisterAllObservers(): Unit
```

Unregisters all DataObservers for this ObjectAdapter.  

### unregisterObserver

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
fun unregisterObserver(observer: ObjectAdapter.DataObserver): Unit
```

Unregisters a DataObserver for data change notifications.  

## Protected functions

### notifyChanged

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
protected fun notifyChanged(): Unit
```

Notifies UI that the underlying data has changed.  

### notifyItemMoved

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
protected fun notifyItemMoved(fromPosition: Int, toPosition: Int): Unit
```

Notifies UI that item at fromPosition has been moved to toPosition.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|--------------------------------|
| `fromPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Previous position of the item. |
| `toPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)   | New position of the item.      |

### notifyItemRangeInserted

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
protected fun notifyItemRangeInserted(positionStart: Int, itemCount: Int): Unit
```

Notifies UI that new items has been inserted.  

|                                          Parameters                                          |
|----------------------------------------------------------------------------------------------|---------------------------------------------|
| `positionStart: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Position where new items has been inserted. |
| `itemCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)     | Count of the new items has been inserted.   |

### notifyItemRangeRemoved

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
protected fun notifyItemRangeRemoved(positionStart: Int, itemCount: Int): Unit
```

Notifies UI that some items that has been removed.  

|                                          Parameters                                          |
|----------------------------------------------------------------------------------------------|----------------------------------------------|
| `positionStart: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | Starting position of the removed items.      |
| `itemCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)     | Total number of items that has been removed. |

### onHasStableIdsChanged

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
protected fun onHasStableIdsChanged(): Unit
```

Called when[setHasStableIds](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#setHasStableIds(boolean))is called and the status of stable ids has changed.  

### onPresenterSelectorChanged

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0)  

```
protected fun onPresenterSelectorChanged(): Unit
```

Called when[setPresenterSelector](https://developer.android.com/reference/kotlin/androidx/leanback/widget/ObjectAdapter#setPresenterSelector(androidx.leanback.widget.PresenterSelector))is called and the PresenterSelector differs from the previous one.