简体   繁体   English

访问 Android 的 Firebase 中的父节点和子节点

[英]Accessing parent and child nodes in Firebase for Android

I want to access these child node data from the Firebase Realtime Database and display them in my app in Android Studio.我想从 Firebase 实时数据库中访问这些子节点数据,并将它们显示在我的 Android Studio 的应用程序中。 How do I write code for that?我该如何为此编写代码?

在此处输入图像描述

It's best to add to your question what Frank asked for, but since you're new, I'll give it a try and write some code for you.最好将 Frank 要求的内容添加到您的问题中,但由于您是新手,我会尝试为您编写一些代码。 So in order to read the value of Age , Email , and Gender , you have to create a reference and attach a listener to it.因此,为了读取AgeEmailGender的值,您必须创建一个引用并为其附加一个侦听器。 Assuming that F1EQ...v302 is the UID of the logged-in user, the code for reading the data should look like this:假设F1EQ...v302是登录用户的 UID,读取数据的代码应该是这样的:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = db.child("All Users").child(uid);
uidRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            String age = snapshot.child("Age").getValue(String.class);
            String email = snapshot.child("Email").getValue(String.class);
            String gender = snapshot.child("Gender").getValue(String.class);
            Log.d("TAG", age + "/" + email + "/" + gender);
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

The result in the logcat will be: logcat 中的结果将是:

18/penny@gm.com/Female

Besides that, it makes more sense to set the Age property to be a number, rather than a string.除此之外,将Age属性设置为数字而不是字符串更有意义。 If you need it later as a string, you can simply convert it into your application code.如果您以后需要它作为字符串,您可以简单地将其转换为您的应用程序代码。

Edit:编辑:

If you want to get the data from all users, indeed you need a loop:如果你想从所有用户那里获取数据,你确实需要一个循环:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference allUsersRef = db.child("All Users");
allUsersRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String age = ds.child("Age").getValue(String.class);
                String email = ds.child("Email").getValue(String.class);
                String gender = ds.child("Gender").getValue(String.class);
                Log.d("TAG", age + "/" + email + "/" + gender);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

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

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