Navigation

Display Collections - Android SDK

Android applications usually display collection data in the UI using the built-in RecyclerView or ListView components. Realm offers adapters to help display data contained in collections of Realm objects that implement the OrderedRealmCollections interface, including RealmResults and RealmList. When you use these adapters, UI components automatically update to reflect changes to underlying Realm objects.

To use the adapters, add the following dependencies to your application level build.gradle file:

dependencies {
implementation 'io.realm:android-adapters:4.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}

The Realm adapters are currently hosted on the JCenter artifact repository. If you have not already registered jcenter as a repository for your Android application, you can add it to your project with the following lines in your project level build.gradle file:

buildscript {
repositories {
jcenter()
}
}
allprojects {
repositories {
jcenter()
}
}

You can find the source code for the Realm Android adapters in the realm/realm-android-adapters GitHub repository.

The examples on this page all use the following data models for a Realm object named Item. This class contains a single piece of string data named "name" and an identifier number named "id":

You can display Realm objects in a ListView by extending RealmBaseAdapter. The adapter uses the ListAdapter interface, so implementation works just like any implementation of ListAdapter, but provides support for automatically-updating Realm objects.

The following example demonstrates how to use a subclass of RealmBaseAdapter to display Item objects in a ListView:

You can display Realm objects in a RecyclerView by extending RealmRecyclerViewAdapter. The adapter extends RecyclerView.Adapter, so implementation works just like any RecyclerView adapter, but provides support for automatically-updating Realm objects.

The following example demonstrates how to use a subclass of RealmRecyclerViewAdapter to display Item objects in a RecyclerView:

Give Feedback

On this page

  • Install Adapters
  • Example Models
  • Display Collections in a ListView
  • Display Collections in a RecyclerView