I am working on Firebase database. How can I retrieve values from database based on email entered by user in text field. Below is my database structure. Kindly help.
You have:
Registration
UserRegistration
12
pushid
key:values
email:emailvalue
To retrieve values always you have to go from top to bottom, so you cannot skip any node.
Only orderByChild(..)
query can skip one node (which means you use this query if you want to get children that are not direct children)
Try this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Registration").child("UserRegistration").child("12");
ref.orderByChild("email").equalTo(emailenteredbyuser).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
String email=datas.child("email").getValue().toString();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
As you can see in the above, the reference passes through every node until node 12
.
Then you use this orderByChild("email").equalTo(emailenteredbyuser)
to get the email entered by the user.
for (DataSnapshot datas : dataSnapshot.getChildren())
this for loop the datas
iterates in inside the direct children of 12
which is the pushid that way you do not need to retrieve the pushid to get the values inside of it.
Try this! here I am running for each loop to get each child.
Ref.child("UserRegistration").child("email").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot:dataSnapshot.getChildren()){
user_reg user=dataSnapshot.getValue(user_reg.class);
System.out.println(user.email);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});