简体   繁体   English

如何从firebase检索相同的数据?

[英]How to retrieve same data from firebase?

I have some problems in retrieving data from firebase.我在从 firebase 检索数据时遇到了一些问题。 I want to retrieve users' emails to listview in which username are same.我想将用户的电子邮件检索到用户名相同的列表视图。 I want to get all users' emails whose names are same as me.我想获取所有与我姓名相同的用户的电子邮件。 I have no idea on how to do that as I am new to android.我不知道如何做到这一点,因为我是 android 新手。

Here is my database structure.这是我的数据库结构。 Greens are uid.绿色是uid。

在此处输入图片说明

Can You Tell Me How To Easily Retrieve Them To A Listview?你能告诉我如何轻松地将它们检索到列表视图中吗?

For this, you can use orderByChild() along with equalTo() to compare the username to what you intend and then get their email.为此,您可以使用orderByChild()equalTo()将用户名与您想要的进行比较,然后获取他们的电子邮件。

What I am saying, looks something like this in code:我在说什么,在代码中看起来像这样:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("details");

  ref.orderByChild("username").equalTo(userNameYouWant).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            for (DataSnapshot ds : dataSnapshot.getChildren()){
                                 email.add(ds.child("email").getValue(String.class)); // email here is the ArrayList where you need to add the email
                            }

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) { // ToDo: don't ignore this, do something for errors

                        }
                  )};                     

This code above, compares all the usernames to the userNameYouWant and if it matches, it adds the value in their email node to the email ArrayList.上面的代码将所有用户名与userNameYouWant进行比较,如果匹配,则将其email节点中的值添加到email ArrayList。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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