I want to read specific data from firebase database. What I am currently doing is here.
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = database.child("profiles/");
myRef.child(phoneNo).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
user = dataSnapshot.getValue(User.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
Method to write to DB
public void writeToDBProfiles(Object data,String phoneNo) {
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = database.child("profiles/" + phoneNo);
myRef.setValue(data);
}
But its returning null... Any help will be appreciated.
After creating a new project, navigate to the Tools option on the top bar. Inside that click on Firebase. After clicking on Firebase, you can get to see the right column mentioned below in the screenshot. Inside that column Navigate to Firebase Realtime 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.
Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events.
Returns a list of objects on each of which you can call val() and key() . To return data from a path simply call the get() method.
If you want to get a specific data using a phone number then you can use a Query like this
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("profiles");
Query phoneQuery = ref.orderByChild(phoneNo).equalTo("+923336091371");
phoneQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
user = singleSnapshot.getValue(User.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "onCancelled", databaseError.toException());
}
});
I think this is the best method.
Ok, I got it sorted out. the above code in the question is perfectly fine and should work . I am explaining for later users who might have the same issue due to mistakes which I made.
the singleSnapshot.getValue(User.class); was unable to cast the result to user class because when uploading the data i.e. in setValue I had my photoUrl equal to null and as a result you can see the photoUrl is not present in my child node. So what I believe is (and maybe someone want correct me on this) that my singleSnapshot.getValue method was unable to cast the result.
So for now I have omitted photoUrl from my User class and will get back to it when I will start working withe relevant module.
Thanks again everyone for all the suggestions and help.
public class User {
String name;
Uri photoURL;
String bloodGroup;
String city;
String country;
double latitude;
double longitude;
boolean availableToDonate;
// Get, set and constructors are obvious here I am just saving some space in the post
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With