1

I am explaining the scenario using the following image: enter image description here

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();
    }
}

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" />

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

5
  • So you want to get the number of children under your database root, which is three?
    – Alex Mamo
    Commented Apr 20, 2021 at 18:13
  • Yes, if I would get number of children from any path that would be more powerful for me, for example after getting students not their children also present like 101, 102, 103, these could be the children of the path /root/students/ Commented Apr 20, 2021 at 18:34
  • So you need a function to count the children of a node?
    – Alex Mamo
    Commented Apr 20, 2021 at 18:42
  • Yes sir, from any node. But if node name or path is not given then we can find the children from root node. Commented Apr 20, 2021 at 18:51
  • Ok, I'll write you an answer right away.
    – Alex Mamo
    Commented Apr 20, 2021 at 19:00

1 Answer 1

0

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);

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);

The result in the logcat will be:

childrenCount: 2
5
  • I changed the question according to your answer but the code is not working, let me know what is the problem? Commented Apr 21, 2021 at 7:05
  • That error happens most likely because of this. It has nothing to do with the code I have provided. Check that answer, do the changes and tell me if it works.
    – Alex Mamo
    Commented Apr 21, 2021 at 7:09
  • Sir, I need the name of children with their count, what could I make the change? Commented Apr 21, 2021 at 7:33
  • You said, "you need a function to count the children of a node". That's what my answer does. If you need something else, that includes some other behavior, please post a new question using its own MCVE, so I and other Firebase developers can help you. Besides that, I rolled back your previous edit, as it's asking for a different issue that is different than the question itself.
    – Alex Mamo
    Commented Apr 21, 2021 at 7:39
  • Let us continue this discussion in chat. Commented Apr 21, 2021 at 7:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.