I am trying to count the number of children in my DB table that meet a certain condition. If the isSeen
column equals false
then I want to count that, if it equals true
I don't want to count it.
Currently it's not working but if I change the query from Query query = usersRef.orderByChild("isSeen").equalTo(true);
to Query query = usersRef.orderByChild("isSeen");
I get a number but it's not the correct way. Can someone please help me ?
{
"DIt5bGqw2WS4eGHNqQJKxZSn3B72": {
"b3vYlKZFrje0e3wHyBlWIK4ooK93": {
"-N8MfBR4gu5G4kMF9fGW": {
"dateAdded": 1659328311354,
"date_time": "Aug-01-2022 12:31:51 AM",
"from": "b3vYlKZFrje0e3wHyBlWIK4ooK93",
"isSeen": false,
"message": "Cool",
"to": "DIt5bGqw2WS4eGHNqQJKxZSn3B72",
"type": "text"
},
"-N8NCgnwX6V7ghfGlcWS": {
"dateAdded": 1659337356887,
"date_time": "Aug-01-2022 3:02:36 AM",
"from": "DIt5bGqw2WS4eGHNqQJKxZSn3B72",
"isSeen": false,
"message": "Hello",
"to": "b3vYlKZFrje0e3wHyBlWIK4ooK93",
"type": "text"
}
}
},
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("Messages");
Query query = usersRef.orderByChild("isSeen").equalTo(true);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
long count = ds.child(firebaseUser.getUid()).getChildrenCount();
//Toast.makeText(MainActivity.this, "In here.", Toast.LENGTH_SHORT).show();
//long count = ds.child("isSeen").getChildrenCount();
Log.d("TAG", "Count:" count);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage());
}
};
query.addListenerForSingleValueEvent(valueEventListener);
CodePudding user response:
Try this
Log.e(dataSnapshot.getKey(),dataSnapshot.getChildrenCount() "");
CodePudding user response:
I think you should try this, it working for me. It read your messages for the first time
usersRef.orderByChild("isSeen").equalTo(true).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Log.d("TAG", "Count:" String.valueOf(dataSnapshot.getChildrenCount()));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});