Kotlin 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:
Kotlin Firebase Realtime Database – Read/Write Data example | Android

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

I. Way to get List of Data

0. Add Firebase to Android App

Please visit Kotlin – Add_Firebase_to_Android_App 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.

kotlin-firebase-db-get-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.
(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 using ChildEventListener.

kotlin-firebase-db-get-list-data-logcat-result

2. Technology

– Gradle 3.0.1
– Android Studio 3.x
– Firebase Android SDK 11.x

3. Project Structure

kotlin-firebase-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.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 MessageActivity

4.5 Android Manifest

4.6 Run & Check result

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

– Sign in, click on TEST MESSAGE, then send new message:
kotlin-firebase-db-get-list-data-result

– Open Firebase Project Console -> Database:
kotlin-firebase-db-get-list-data-console-result

– Android Logcat after clicking on Back button:
kotlin-firebase-db-get-list-data-logcat-result

– You can check ChildEventListner by adding, changing, removing, moving any Message node on Firebase console.

III. Source code

Kotlin-FirebaseRealtimeDB-get-list-data

By JavaSampleApproach | December 19, 2017.

Last updated on July 13, 2018.


Related Posts


Got Something To Say:

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

*