Kotlin Firebase Realtime Database – Display List of Data with FirebaseRecyclerAdapter | Android

In previous post, we had known how to read/write list of data object. Today, we’re gonna look at way to display List of Data in an Android App with FirebaseUI FirebaseRecyclerAdapter.

Related Articles:
Kotlin Firebase Realtime Database – Read/Write Data example | Android
Kotlin Firebase Realtime Database – Get List of Data example | Android

I. FirebaseUI Database

To use the FirebaseUI to display list of data, we need:
– Java class for data object (Model)
– Java class for holding UI elements that match with Model’s fields (ViewHolder and layout)
– Custom RecyclerView adapter to map from a collection from Firebase to Android (FirebaseRecyclerAdapter)
RecyclerView object to set the adapter to provide child views on demand.


1. Model and ViewHolder

Model class is a class that represents the data from Firebase:

ViewHolder layout (R.layout.item_message) with UI items that correspond to Model fields:

ViewHolder class contains Android UI fields that point to layout items:

2. FirebaseRecyclerAdapter subclass

We need a subclass of the FirebaseRecyclerAdapter and implement its populateViewHolder() method:

Now look at these lines of code:

– We tell FirebaseRecyclerAdapter object to use Message.class when reading from the database.
– Each Message will be displayed in a R.layout.item_message (that has 3 TextView elements: tvAuthorItem, tvTimeItem, tvBodyItem).
– We indicate class for ViewHolder
– We can just give reference to database node or sort/filter data by using Query:

FirebaseRecyclerAdapter will call populateViewHolder() method for each Model it finds in database. It passes us the Model and a ViewHolder.
So what we should do is map the fields from model to the correct TextView items:

We can override onChildChanged() method to do something every time a Child in database has changed its value (add, update…).

3. RecyclerView

Now we set the adapter for RecyclerView object to provide child views on demand:

Remember to call adapter cleanup() method to stop listening for changes in the Firebase database:

4. Dependency

build.gradle file (App-level)

II. Practice

1. Goal

We will build an Android App that can:
– create Account, sign in/sign out for Firebase Authentication.
– read/write user to Firebase Realtime Database.
(2 lines above come from this Post).
– write Message item to 2 nodes (/messages/$key and /user-messages/$userid/$key) at the same time, then read list of all Message items. (from this Post)
– display list of Messages using FirebaseUI FirebaseRecyclerAdapter.


2. Technology

– Gradle 3.0.1
– Android Studio 3.x
– Firebase Android SDK 11.x
– Firebase UI Database 2.3.0

3. Project Structure


LoginActivity is for Authentication, then user can enter MessageActivity to send Message to Firebase Realtime Database and show list of Message data.

4. Step by step
4.1 Create Android Project

– Generate new Android Project with package com.javasampleapproach.kotlin.firebase.realtimedb.
– Follow this instruction to add Firebase Auth and Realtime DB.

4.2 Model

4.3 LoginActivity

In this tutorial, we don’t explain way to authenticate an user again. To know how to implement Firebase Authentication App Client, please visit:
Kotlin Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android

4.4 ViewHolder

4.5 MessageActivity

4.6 Run & Check result

– Use Android Studio, build and Run your Android App:


– Firebase Console:

III. Source code


By grokonez | December 20, 2017.

Last updated on July 13, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *