I am trying to read \"specific\" data from my firebase
realtime database. I have seen examples of getting all data from a table and going through it to find the
You need to do the query:
orderByChild("FirstName").equalTo(name);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("user");
reference.orderByChild("firstName").equalTo(name).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String familyname=datas.child("familyName").getValue().toString();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Assuming you have this database:
user
randomid
firstName: John
familyName: familyName_here
Another way is to use the userid to be able to retrieve the data, but as I see in the question you are saving a random id using push()
.
You need to retrieve the userid:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
then add it to the database:
myRef.child(userid).setValue(user);
then to retrieve data only for John:
FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("user");
reference.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {..}
Query specific_user = myRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
specific_user.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//here you will get the data
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
You have to collect the response from firebase to somePOJO Class
follow link : https://firebase.google.com/docs/database/admin/retrieve-data
As you can see in onDataChange() the response is collected in Post.class which is POJO class.