如何根据用户在文本字段中输入的电子邮件从Firebase数据库中检索值? [英] How can I retrieve values from Firebase database based on email entered by user in text field?

查看:268
本文介绍了如何根据用户在文本字段中输入的电子邮件从Firebase数据库中检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase数据库。如何根据用户在文本字段中输入的电子邮件从数据库中检索值。下面是我的数据库结构。请帮忙。

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.

这是我的代码:

 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();
                            }


推荐答案

您有:

 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.

只有 orderByChild(..)查询可以跳过一个节点(这意味着如果你想让不是直接孩子的孩子,你可以使用这个查询)

Only orderByChild(..) query can skip one node (which means you use this query if you want to get children that are not direct children)

试试这个:

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

       }

 });

正如您在上面所看到的,引用遍历每个节点,直到节点 12

As you can see in the above, the reference passes through every node until node 12.

然后你使用这个 orderByChild(email).qualTo(emailenteredbyuser)获取用户输入的电子邮件。

Then you use this orderByChild("email").equalTo(emailenteredbyuser) to get the email entered by the user.

for(DataSnapshot datas:dataSnapshot.getChildren())这个for循环数据 12 的直接子元素内迭代,这是你不需要的方式检索pushid以获取其中的值。

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.

这篇关于如何根据用户在文本字段中输入的电子邮件从Firebase数据库中检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆