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 :
- addValueEventListener
- addListenerForSingleValueEvent
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
private DatabaseReference root = FirebaseDatabase.getInstance().getReference().child(name_of_node); String my_value1,my_value2; private ArrayList<My_Model> my_list = new ArrayList<My_Model>(); root.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { Iterator i = dataSnapshot.getChildren().iterator(); while (i.hasNext()) { my_value1 = (String) ((DataSnapshot) i.next()).getValue(); my_value2 = (String) ((DataSnapshot) i.next()).getValue(); My_Model my_model = new My_Model(my_value1, my_value2); my_list.add(my_model); } } @Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { } @Override public void onChildRemoved(DataSnapshot dataSnapshot) { } @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { } @Override public void onCancelled(DatabaseError databaseError) { } }); |
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 :
- onDataChange : this will notify you of any changes in the data at a particular path
- 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 : )