Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase, simply get one child object's data

I have been looking for a way to get one child object's data in Android Firebase.

I have found things like Firebase retrieve child Android. All the solutions are suggesting using a "ChildEventListener", however I need to get this data at this moment, not when it is moved, deleted, updated, etcetera.

My data is kept in https://.firebaseio.com/users//creation as a string. I figure there must be some simple way to access that without needing to do too much, because if I copy the exact URL to my browser, I can see the: 'creation: "2015/05/31 21:33:55"' right there in my "Firebase Forge Dashboard".

How can I access this without a listener?

like image 934
Bobby Avatar asked Jun 01 '15 02:06

Bobby


People also ask

What is DataSnapshot in Firebase?

firebase. database. DataSnapshot. A DataSnapshot contains data from a Database location. Any time you read data from the Database, you receive the data as a DataSnapshot .

What is addListenerForSingleValueEvent?

public void addListenerForSingleValueEvent (ValueEventListener listener) Add a listener for a single change in the data at this location. This listener will be triggered once with the value of the data at the location.

How do I get data from Firebase database?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.


2 Answers

Firebase listeners fire for both the initial data and any changes.

If you're looking to synchronize the data in a collection, use ChildEventListener. If you're looking to synchronize a single object, use ValueEventListener. Note that in both cases you're not "getting" the data. You're synchronizing it, which means that the callback may be invoked multiple times: for the initial data and whenever the data gets updated.

This is covered in Firebase's quickstart guide for Android. The relevant code and quote:

FirebaseRef.child("message").addValueEventListener(new ValueEventListener() {   @Override   public void onDataChange(DataSnapshot snapshot) {     System.out.println(snapshot.getValue());  //prints "Do you have data? You'll love Firebase."   }   @Override   public void onCancelled(DatabaseError databaseError) {           } }); 

In the example above, the value event will fire once for the initial state of the data, and then again every time the value of that data changes.

Please spend a few moments to go through that quick start. It shouldn't take more than 15 minutes and it will save you from a lot of head scratching and questions. The Firebase Android Guide is probably a good next destination, for this question specifically: https://firebase.google.com/docs/database/android/read-and-write

like image 113
Frank van Puffelen Avatar answered Oct 18 '22 00:10

Frank van Puffelen


You don't directly read a value. You can set it with .setValue(), but there is no .getValue() on the reference object.

You have to use a listener. If you just want to read the value once, you use ref.addListenerForSingleValueEvent().

Example:

Firebase ref = new Firebase("YOUR-URL-HERE/PATH/TO/YOUR/STUFF"); ref.addListenerForSingleValueEvent(new ValueEventListener() {    @Override    public void onDataChange(DataSnapshot dataSnapshot) {        String value = (String) dataSnapshot.getValue();         // do your stuff here with value     }     @Override    public void onCancelled(FirebaseError firebaseError) {     } }); 

Source: https://www.firebase.com/docs/android/guide/retrieving-data.html#section-reading-once

like image 36
lenooh Avatar answered Oct 17 '22 23:10

lenooh