Redeem Now
Read More
Read now

Reading Data from your Firebase Realtime Database


In last blog , we discussed how to write data to your  firebase realtime database from your android app.

Firebase database provides you three listeners, through which you can retrieve the data.

These are :

  1. addValueEventListener
  2. addListenerForSingleValueEvent
  3. addChildEventListener

A brief insight to these methods,

addValueEventListener : This listener helps you to detect the change in the data at a particular path (including the children at that particular path). This listener keeps listening till your app is connected to the firebase.

addListenerForSingleValueEvent :This listener helps you to detect the change in the data at a particular path (including the children at that particular path). This listener listens exactly once and provides you the data as per the change until this event was triggered.

addChildEventListener : When working with the lists of data, we should  use this listener rather than the above mentioned two. This listener helps you listen to the events related to the particulars of a child node of the root. The methods in this listener are triggered in response to specific operations that happen to the children of a node from an operation.

For more details on these , please refer to the Firebase documents or click here .

To read data, i have used the addChildEventListener, so will discuss that in more detail and along with the code snippet.

All you need to do is to get to the exact node on your realtime database and attach a listener to it. You will recieve the data as an object of DataSnapshot, and you need to extract the data from this as per your need and add to your list.

Code :

Similary you can change any of the methods in this listener as per your need.

The names of the methods itself makes pretty clear on what exactly would be the scope of the method.

You can also use “addValueEventListener” or “addListenerForSingleValueEvent” , in these you will have to override two methods, namely :

  1. onDataChange : this will notify you of any changes in the data at a particular path
  2. onCancelled : this will help you handle the error while reading data.

“onDataChange” will return the data as an object of DataSnapshot and you can use it in a similar manner as depicted in the code.

That’s all !

You really need to structure your database well so that you can read the data with ease.

For seeing last blog , please click here

Thanks !

Keep coding and keep sharing : )

. . .


Add Your Comment

Be the first to comment.

Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again