Reputation: 101
I am trying to count the amount of data that has been entered into a specific child into a firebase database.
public void onDataChange(DataSnapshot dataSnapshot) {
if (!registerVote) {
String user = dbAuth.getCurrentUser().getEmail().toString();
myFB.child(uniquePostID).child(dbAuth.getCurrentUser().getUid()).setValue(user);
final Animation likeRotate = AnimationUtils.loadAnimation(getActivity(), R.anim.rotatelike);
final Animation likeZoom = AnimationUtils.loadAnimation(getActivity(), R.anim.zoomlike);
AnimationSet multiLike = new AnimationSet(false);
multiLike.addAnimation(likeRotate);
multiLike.addAnimation(likeZoom);
viewHolder.postVoteBtn.startAnimation(multiLike);
long numOfLikes = dataSnapshot.child(uniquePostID).child(dbAuth.getCurrentUser().getUid()).getChildrenCount();
viewHolder.display.setText(Long.toString(numOfLikes));
Log.d("LONG", Long.toString(numOfLikes));
registerVote = false;
}
I use the data snapshot to add to the database at child
myFB.child(uniquePostID).child(dbAuth.getCurrentUser().getUid()).setValue(user);
but then when I use
long numOfLikes = dataSnapshot.child(uniquePostID).child(dbAuth.getCurrentUser().getUid()).getChildrenCount();
viewHolder.display.setText(Long.toString(numOfLikes));
It always shows as 0 and does not register anything. Each time a different account likes a post, a name is added to the child that matches the post ID. I want to use the getChildrenCount() to count every name that has like a certain post. I have also noticed that getChildrenCount() will perform the count on preloaded data only, my need is to have the data be retrieved in real time.
{
"Blog" : {
"-KgM1HzLwU1RMx13Z41J" : {
"desc" : "Feeling good",
"title" : "Smashed the gym"
},
"-KgM1NlrcBTYUhUYWYBR" : {
"desc" : "Really didn't feel like leaving the house today, so I played call of duty all day.",
"title" : "Stayed inside"
},
"-KgM1PfgkzSeBxUWi_3t" : {
"desc" : "Nothing helps clear your mind like time with friends",
"title" : "Spending time with friends"
},
"-KgM1Wg54bERKNUyKnE2" : {
"desc" : "But don't be afraid to dream big",
"title" : "Everyone starts small"
},
"-KgM1Zojv7q5FhRi_a3u" : {
"desc" : "Finally managed to break my personal best I'd had for months!",
"title" : "New gym PB"
},
"-KgOmll6K6o_sv1JEUcn" : {
"desc" : "A years worth of work comes to an end today, pretty nervous!",
"title" : "Big presentation today"
},
"-KgP1bC4BlAe88XyN_VV" : {
"desc" : "time to present",
"title" : "let's go"
},
"-KgPE7DI2j7R1QLiL3Bb" : {
"Likers" : {
"stg15QKZFhNmTCYrgL5PtQ4wxJf2" : "Joe Bloggs"
},
"desc" : "You've got this",
"title" : "Stay positive"
},
"-KgPG7siKLWKzij1Lu3d" : {
"desc" : "John is looking at this right now",
"title" : "Honours presentation"
},
"-Kne46iBe6ooNFKTv_8w" : {
"desc" : "bugs?",
"email" : "Joe Bloggs",
"title" : "new"
}
},
"Engagement" : {
"-KgP1bC4BlAe88XyN_VV" : {
"Likers" : {
"JXxWjn9nvQcNsvaf3CO7HqUNKKi2" : "Bob Smith",
"stg15QKZFhNmTCYrgL5PtQ4wxJf2" : "Joe Bloggs"
},
"stg15QKZFhNmTCYrgL5PtQ4wxJf2" : "Bob Smith"
}
}
}
Upvotes: 2
Views: 5327
Reputation: 1
Please use this code:
long numOfLikes = dataSnapshot
.child("Engagement")
.child(uniquePostID)
.child("Likers")
.getChildrenCount();
Hope it helps.
Upvotes: 1
Reputation: 600131
I have a feeling you're missing the Likers
level from your JSON in your code:
long numOfLikes = dataSnapshot
.child(uniquePostID)
.child("Likers")
.child(dbAuth.getCurrentUser().getUid())
.getChildrenCount();
Upvotes: 0