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:
Firebase Realtime Database – Read/Write Data example | Android
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: tv_author, tv_time, tv_body).
– 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

To add Support Library to Android project, open the build.gradle file (project-level).
Make sure that the repositories section includes a maven section with the “” endpoint:

For 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 2.3.3
– Android Studio 2.x
– Firebase Android SDK 11.x
– Firebase UI Database 2.1.1

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.firebaserealtimedb.
build.gradle (project-level):

build.gradle (App-level):

4.2 Create Firebase Project & Add Firebase Config file

– Follow this guide to generate google-services.json file and move it into your Android App root directory. You don’t need to have SHA-1 Key in this example, just leave it blank.

– Make sure that package_name in google-services.json has a correct value according to:
+ applicationId in build.gradle (App-level).
+ package in AndroidManifest.xml.
In this case, it is com.javasampleapproach.firebaserealtimedb.


4.3 Enable Firebase Auth

Go to Your Firebase Project Console -> Authentication -> SIGN-IN METHOD -> Enable Email/Password.

4.4 Model

4.5 LoginActivity

To know how to implement Firebase Authentication App Client, please visit:
Firebase Authentication – How to Sign Up, Sign In, Sign Out, Verify Email | Android

In this tutorial, we don’t explain way to authenticate an user again.

4.6 ViewHolder

4.7 MessageActivity

4.8 Run & Check result

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


– Firebase Console:

III. Source code


Related Posts

Got Something To Say:

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