Firebase Realtime Database – Get List of Data example | Android

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

Related Article:
Firebase Realtime Database – Read/Write Data example | Android

More Practice:
Firebase Realtime Database – Display List of Data with FirebaseRecyclerAdapter | Android

I. Way to get List of Data

0. Add Firebase to Android App & Enable Firebase Auth

Please visit previous Post for details.

1. Write to specific child of a node

Before retrieving all data item from a list, we need a node that contains child nodes as data items. So we should write data to each node at first.

Assume that we have a class like this:

Use the updateChildren() method, we can write data to specific child node without overwriting other child nodes (by specifying a path for the key):

The push() method generates a key every time a new child is added.

fireabase-realtime-db-list-data-console-result

2. Read List of Data

We can retrieve List that contains all Data items by collecting each item in Child Event onChildAdded() call back.

There are some Child Events which are triggered when a child being updated through updateChildren() method. In order to listen for Child Events, attach a ChildEventListener to DatabaseReference:

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 previous Post)
– write Message item to 2 nodes (/messages/$key and /user-messages/$userid/$key) at the same time.
– read list of Message items.

fireabase-realtime-db-list-data-demo

2. Technology

– Gradle 2.3.3
– Android Studio 2.x
– Firebase Android SDK 11.x

3. Project Structure

firebase-realtime-db-structure

LoginActivity is for Authentication, then user can change to MessageActivity to send Message to Firebase Realtime Database and retrieve List of them.

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.

firebase-realtime-db-add-app

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 MessageActivity

4.7 Android Manifest

4.8 Run & Check result

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

fireabase-realtime-db-list-data-demo

– Firebase Console:
fireabase-realtime-db-list-data-console-result

– Android Logcat after clicking on Back button:

– Click on TEST MESSAGE button to go to MessageActivity, insert new Message and click on Send button:
fireabase-realtime-db-list-data-app-result

Android Logcat:

And after clicking on Back button:

– Go to Firebase Console, change the message body:
fireabase-realtime-db-list-data-console-result-change

Android Logcat:

And after clicking on Back button:

III. Source code

FirebaseRealtimeDB-get-list-data


Related Posts


Got Something To Say:

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

*