Reputation: 11
I am making an attendance app and I need to count the particular student attendance from the Firebase. I need to count the number of "prof" occurrence and I am new to this.
DatabaseReference attendance = FirebaseDatabase.getInstance().getReference("Attendance");
attendance.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
totaldays++; // total class days
if(snapshot.child("name").getValue(String.class).equals("prof")){
//count no of days present
}
Log.e(snapshot.getKey(), snapshot.getChildrenCount() + "");
}
Log.e(TAG, totaldays + "");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});
But the result is showing null
value.
Upvotes: 1
Views: 1761
Reputation: 1473
its very simple to count any field child in the root reference
for example you have a root name attendance then you count to name where type is professor
databaseReference = FirebaseDatabase.getInstance().getReference().child("Attendance").child(istrustnumberavailable);
databaseReference.limitToFirst(5);
Query query = databaseReference.orderByChild("name").equalTo("prof");
ChildEventListener removedListener = query.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if (dataSnapshot.exists()){
long count= dataSnapshot.getChildrenCount();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if (dataSnapshot.exists()){
long count= dataSnapshot.getChildrenCount();
}
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
long count= dataSnapshot.getChildrenCount();
}
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 0
Reputation: 138824
In order to solve this, you need to change your database structure. You cannot query your database since your nodes are dynamically generated. As @FrankvanPuffelen mentioned in his comment, the nodes that are causing you troubles are: 04Jul2018
with dcbch...
and 05Jul2018
with hello
. Because Firebase can only return results that are at a fixed path under each child of the node, to solve this, I recommend you create the date as a property. This is how it can be done and this is how your database structure should look like:
Firebase-root
|
--- dcbch..._dkjh...
|
--- Prof
|
--- email: "[email protected]"
|
--- name: "prof"
|
--- present: true
|
--- date: 1530784439 //Current timestamp
And here is the corresponding query:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.orderByChild("Prof/name").equalsTo("prof");
Now you can attach a listener and use getChildrenCount()
method on the DataSnapshot
object to get the number of occurrence.
Upvotes: 1
Reputation: 193
If there is a prof occurrence you can add that child data in a list. In this way you can get the number of childs by using list.size()
and also use this list of data in other operations if you have.
Upvotes: 0