Display Collections - Java SDK
On this page
Android apps often populate the UI using
RecyclerView
or ListView components.
Realm offers adapters to display realm object
collections. These collections implement
the OrderedRealmCollections
interface. RealmResults
and RealmList are examples of these adaptors.
With these adapters, UI components update when your app changes
Realm objects.
Install Adapters
Add these dependencies to your application level build.gradle
file:
dependencies { implementation 'io.realm:android-adapters:4.0.0' implementation 'androidx.recyclerview:recyclerview:1.1.0' }
Realm hosts these adapters on the
JCenter
artifact repository. To use jcenter
in your Android app, add it to your
project-level build.gradle
file:
buildscript { repositories { jcenter() } } allprojects { repositories { jcenter() } }
Tip
See also:
Source code: realm/realm-android-adapters on GitHub.
Example Models
The examples on this page use a Realm object named Item
.
This class contains a string named "name" and an identifier number named
"id":
Display Collections in a ListView
Display Realm objects in a
ListView by extending
RealmBaseAdapter.
The adapter uses the ListAdapter
interface. Implementation works
like any ListAdapter
. This provides support for automatically-updating
Realm objects.
Subclass RealmBaseAdapter
to display
Item objects in a ListView
:
Display Collections in a RecyclerView
Display Realm objects in a
RecyclerView
by extending RealmRecyclerViewAdapter.
The adapter extends RecyclerView.Adapter
. Implementation works like any
RecyclerView
adapter. This provides support
for automatically-updating Realm objects.
Subclass RealmRecyclerViewAdapter
to display
Item objects in a RecyclerView
: