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.

1 Answers1

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
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I changed the question according to your answer but the code is not working, let me know what is the problem? – sanjay gupta Apr 21 '21 at 07:05
  • That error happens most likely because of [this](https://stackoverflow.com/questions/56099945/java-lang-nosuchmethoderror-no-virtual-method-settokenprovider). 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 Apr 21 '21 at 07:09
  • Sir, I need the name of children with their count, what could I make the change? – sanjay gupta Apr 21 '21 at 07: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](https://stackoverflow.com/help/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 Apr 21 '21 at 07:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231404/discussion-between-sanjay-gupta-and-alex-mamo). – sanjay gupta Apr 21 '21 at 07:44