Tavinder Singh
Tavinder Singh

Reputation: 420

How to get total number of child from firebase database

I have created an android app which stores the value of User class in the following manner.

{
  "users" : {
    "Om8VuPSCcvg7d5jsYtZvPTWpm5o1" : {
      "email" : "[email protected]",
      "name" : "Ajay",
      "uId" : "Om8VuPSCcvg7d5jsYtZvPTWpm5o1"
    },
    "v1dHuFXkfJYt6fYppICQS6rjxiw2" : {
      "email" : "[email protected]",
      "name" : "Tavinder Singh",
      "uId" : "v1dHuFXkfJYt6fYppICQS6rjxiw2"
    }
  }
}

My User class:

@IgnoreExtraProperties
public class User implements Parcelable {
    public String uId;
    public String email;
    public String name;

    public User() {

    }

    public User(String uId, String email, String name) {
        this.uId = uId;
        this.email = email;
        this.name = name;
    }

    protected User(Parcel in) {
        uId = in.readString();
        email = in.readString();
        name = in.readString();
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(uId);
        parcel.writeString(email);
        parcel.writeString(name);
    }
}

I am using the following code to save the data

FirebaseUser user = mAuth.getCurrentUser();
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database.child("users").child(user.uId).setValue(user);

But when I am trying to count the total number of child the "users" have, all I am getting is 0. I am using the following code to get the total number of child:

userReference = FirebaseDatabase.getInstance().getReference().child("users");
userReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    num = dataSnapshot.getChildrenCount();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

I want to know that how I can get the the total number of child.

Upvotes: 2

Views: 4340

Answers (1)

Bruno Ferreira
Bruno Ferreira

Reputation: 1571

Try this:

userReference = FirebaseDatabase.getInstance().getReference("users");
userReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    num = dataSnapshot.getChildrenCount();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

Upvotes: 6

Related Questions