-1

I am new to firebase, I was trying to get the size of data in a Users where Users have different records and every record have two properties Name and Contact. I want to get the size of records in cont. I am trying to get this in android application using java.

structure of database is looks like this

Users

   Records
        Name
        Contact

I want to get the count of Records.

Fahad
  • 37
  • 1
  • 10

4 Answers4

4

Assuming that you have a database structure that looks like this:

Firebase-root
    |
    --- users
          |
          --- uidOne
          |    |
          |    --- name: "NameOne"
          |    |
          |    --- contact: "ContactOne"
          |
          --- uidTwo
               |
               --- name: "NameTwo"
               |
               --- contact: "ContactTwo"

To get the number of users, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        long count = dataSnapshot.getChildrenCount();
        Log.d("TAG", "count= " + count);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
    }
};
usersRef.addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
3

To get counts there is only one line dataSnapshot.getChildrenCount();

Please refer to the answer below

ref.addListenerForSingleValueEvent(new ValueEventListener() { //ref will be your desired path where you want to count children
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (dataSnapshot.getValue() != null) {   // Check for data snapshot has some value
                            dataSnapshot.getChildrenCount()  // check for counts of data snapshot children
                           }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
Deep Patel
  • 2,584
  • 2
  • 15
  • 28
0

Use the value listener

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();

//You can use the single or the value.. depending if you want to keep track
thismyRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot snap: dataSnapshot.getChildren()) {
        Log.e(snap.getKey(),snap.getChildrenCount() + "");
    }
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
Ahmad Ayyaz
  • 774
  • 8
  • 25
0

All you need is DataSnapshot.

Also have a look at this SO question

kgandroid
  • 5,507
  • 5
  • 39
  • 69