我们如何使用Android代码获取根节点的子节点数? [英] How do we get number of child nodes of root node using Android code?

查看:59
本文介绍了我们如何使用Android代码获取根节点的子节点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下图像解释该场景:

I am explaining the scenario using the following image:

由此,我想获取节点的名称和数量.从此图像中,我想获取节点的名称(公司,GovDepartment和学生)及其计数.例如,存在3个存储为Company,GovDepartment和Student的节点,我怎么能得到它?我怎样才能完成它?以下android代码需要完成:

from that, I would like to get the name and number of nodes. From this image, I would like to get nodes' names (Company, GovDepartment, and students) and their count number. for example, there are 3 numbers of nodes are present which stored as Company, GovDepartment, and students, how could I get it? how could I make it complete? the following android code needs to complete:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {
    EditText t1,t2,t3,t4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void process(View v){
        
        dataholder obj = new dataholder(name,course,duration);
        FirebaseDatabase db = FirebaseDatabase.getInstance();
        DatabaseReference node = db.getReference();
    }
}

此处的OnClick按钮调用的方法为:

here the method called by a OnClick button as:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="process"
    android:text="Read and count the nodes"
    android:textSize="25dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.491"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.66"
    tools:ignore="MissingConstraints,OnClick" />

您可以使用其他布局显示从Firebase数据库中获取的数据.

you may use the another layout to show the fetched data from the firebase database.

推荐答案

要计算节点的子节点数,无论它是数据库的根节点还是任何其他节点,请使用以下方法:

To count the number of children of a node, no matter if it's the root of the database or any other node, please use the following method:

private 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);
            } else {
                Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
            }
        }
    });
}

要计算您的根节点的子节点数,请像上面这样调用上述方法:

To count the number of children of your root node, call the above method like so:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
printChildrenCount(rootRef);

logcat中的结果将是:

The result in the logcat will be:

childrenCount: 3

计算学生"的孩子数节点,请像上面这样调用上述方法:

To count the number of children of "students" node, call the above method like so:

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

logcat中的结果将是:

The result in the logcat will be:

childrenCount: 2

这篇关于我们如何使用Android代码获取根节点的子节点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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