How to get value from highest / last level node using ChildEventListener function from Firebase?

I have several lines stored by a specific link: mReference.child(rID).child(userID2) that I want to get using childEventListener since I do some task also when these lines are deleted, and this is only possible with onChildRemoved from ChildEventListener .

Here is what I tried:

 mReference.child(rID).child(userID2).addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { Log.d("dataHEre", dataSnapshot.getValue().toString()); } } 

The problem is that I cannot get the data using keys here, since the dataHEre is dataHEre out:

 D/dataHEre: 28.8419556 D/dataHEre: 78.779063 D/dataHEre: 3 D/dataHEre: Railway Colony D/dataHEre: Sleep D/dataHEre: 12:36 AM D/dataHEre: Superman 

what are the values ?

So, I want to know how I can get the data here using keys and ChildEventListener and then assign the received data to different rows?

0
source share
1 answer

I think you are doing your request wrong. onChildAdded method returns you every child of a certain value (userID2, I suppose). If this is what you want, just use onValueEventListener() to retrieve the entire dataSnapshot of your userId2 node every time it changes.

If you want to retrieve data only once, you should use onSingleValueEventListener() . And OnChildEvent() used to retrieve data lists where you want to track each child separately. For example, if you attach OnChildEventListener to mReference.child(rID) , you will get onChildAdded for each userId, which is very useful, for example, fill in the RecyclerView or ListView to update each element separately with your Firebase data.

If I'm not mistaken, you just get updates to your userId2 link, in this case attach OnValueEventListener to this link, and you will get a call every time the value is changed, deleted or added.

  firebaseDatabaseRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { customPOJO customPOJO = dataSnapshot.getValue(YourCustomPOJO.class); customPOJO.getName(); customPOJO.getEmail(); customPOJO.getfavoriteFood(); //and so on.... } @Override public void onCancelled(DatabaseError databaseError) { } }); 
0
source

Source: https://habr.com/ru/post/1014448/


All Articles