Skip to content Skip to sidebar Skip to footer

Unable To Retrieve The Value From Firebase Database

I am trying to retrieve values from my firebase database. It is displaying the values in onDataChange() function but unable to display the values out of it . Code: public Records r

Solution 1:

As I mentioned in my comment call to Firebase is asynchronous. If you want your method to return Records object you should use callback:

publicvoidreadtraditional(String email, MyCallback callback)
{
    final Records record = newRecords();
    final String[] name = newString[1];

    myRef.child(email).child("name").addValueEventListener(newValueEventListener() {
        @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
            name[0] = dataSnapshot.getValue().toString() ;                
            record.setName(name[0]);
            callback.onSuccess(record)
        }
        @OverridepublicvoidonCancelled(DatabaseError databaseError) {
        }
    });   

}

Interface:

publicinterfaceMyCallback{
           voidonSuccess(Records record)
}

And you can call this method like this:

readtraditional(mystring,newMyCallback{
        @OverridevoidonSuccess(Records record){
            //do whatever you want
        }
});

Post a Comment for "Unable To Retrieve The Value From Firebase Database"