简体   繁体   中英

How to retrieve same data from firebase?

I have some problems in retrieving data from 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.

Here is my database structure. Greens are 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.

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.

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