Java 如何使用Android代码获取根节点或任何节点的子节点的名称?

Java 如何使用Android代码获取根节点或任何节点的子节点的名称?,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,从下面的代码中,我可以得到根节点的子节点数,但我还想得到它们的名称。我怎么能得到它?我应该在下面的代码中做什么更改 package com.example.application_for_curd; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import androidx.annotation.NonNull; import com.go

从下面的代码中,我可以得到根节点的子节点数,但我还想得到它们的名称。我怎么能得到它?我应该在下面的代码中做什么更改

package com.example.application_for_curd;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.android.gms.tasks.*;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        printChildrenCount(rootRef);
    }


    private static final String TAG = "MainActivity";
    public void printChildrenCount(DatabaseReference ref) {
        ref.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DataSnapshot> task) {
                if (task.isSuccessful()) {
                    long childrenCount = task.getResult().getChildrenCount();
                    Log.d(TAG, "childrenCount: " + childrenCount);
                    for (int i=0;i<childrenCount;i++)
                    {
//                        Log.d(TAG, "childrenName: " + childrenname); // need to involve code here to get children name
                    }
                } else {
                    Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
                }
            }
        });
    }
}
package com.example.application\u for\u curd;
导入androidx.appcompat.app.appcompat活动;
导入android.os.Bundle;
导入android.util.Log;
导入androidx.annotation.NonNull;
导入com.google.firebase.database.DataSnapshot;
导入com.google.firebase.database.DatabaseReference;
导入com.google.firebase.database.FirebaseDatabase;
导入com.google.android.gms.tasks.*;
公共类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseReference rootRef=FirebaseDatabase.getInstance().getReference();
printChildrenCount(rootRef);
}
私有静态最终字符串TAG=“MainActivity”;
public void printChildrenCount(数据库参考ref){
ref.get().addOnCompletListener(新的OnCompletListener()){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
long childrenCount=task.getResult().getChildrenCount();
Log.d(标签“childrenCount:+childrenCount”);
对于(int i=0;i,根据您前面问题中的内容,要获取根节点中存在的子节点的名称,请使用以下代码行:

public void printChildrenCount(DatabaseReference ref) {
    ref.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DataSnapshot> task) {
            if (task.isSuccessful()) {
                long childrenCount = task.getResult().getChildrenCount();
                Log.d(TAG, "childrenCount: " + childrenCount);
                for (DataSnapshot child : task.getResult().getChildren()) {
                    String childName = child.getKey();
                    Log.d(TAG, "childName: " + childName);
                }
            } else {
                Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
            }
        }
    });
}
logcat中的结果将是:

Company
GodDepartments
students
需要注意的是,为了能够获得子节点的名称(实际上是节点的键),您需要使用对“.getChildren()”方法的调用来循环“DataSnapshot”对象

例如,要在“students”ref下获得孩子的名字,请使用以下命令将其踢出:

DatabaseReference studentsRef = FirebaseDatabase.getInstance().getReference().child("students");
printChildrenCount(studentsRef);
DatabaseReference studentsRef = FirebaseDatabase.getInstance().getReference().child("students");
printChildrenCount(studentsRef);