简体   繁体   中英

How can I retrieve values from Firebase database based on email entered by user in text field?

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.

数据库结构

Here is my code:

 Ref = database.getReference("Registeration"); 
                        Ref.orderByChild("UserRegistration/email").equalTo(emailval).addChildEventListener(new ChildEventListener() {
                            @Override
                            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                                user_reg user=dataSnapshot.getValue(user_reg.class);
                                Toast.makeText(getApplicationContext(), user.toString(), Toast.LENGTH_SHORT).show();
                            }

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) {

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM