Reputation: 101
I'm trying to get the children count of the child circled in red. I want to get 2 but I end up getting 5. Is there a way I can get only the count of the sub children circled in green so that I can get 2 instead of 5? Any help will be greatly appreciated.
This is the code that I am Using.
private void getComments(String postid, String commentid, final TextView commentsonComment){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("comments").child(postid).child(commentid);
reference.addValueEventListener(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
long commentNo = dataSnapshot.getChildrenCount();
int numberOfComments = (int) commentNo;
if (numberOfComments > 1 ){
commentsonComment.setText("View all "+ dataSnapshot.getChildrenCount() + " comments");
}else if (numberOfComments == 0){
commentsonComment.setText( "No comments yet");
}else {
commentsonComment.setText("View "+ dataSnapshot.getChildrenCount() + " comment");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Upvotes: 0
Views: 133
Reputation: 138834
I'm trying to get the children count of the child circled in red. I want to get 2 but I end up getting 5.
That's the normal behavior since under -MEMFSGefNqrJXEEk3f
node, there are exactly 5 children. It doesn't matter if the properties are of type String or objects, like the one you want to count, all are considered children of that node. You have two solutions for solving this matter. The first one would be to check if the properties are objects by calling getChildrenCount()
, as in the following lines of code:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("comments").child(postid).child(commentid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
if (ds.getChildrenCount() > 1) {
String commentText = ds.child("commenttext").getValue(String.class);
Log.d("TAG", commentText);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage()); //Don't ignore errors!
}
};
reference.addListenerForSingleValueEvent(valueEventListener);
The result in the logcat being:
hello here
hello here two
The second option that you have is to isolate those comments in a separate node, like this:
Firebase-root
|
--- comments
|
--- postid
|
--- commentid
|
--- comments (newly added)
|
--- -MEMF ... YjIO
|
--- commenttext: "hello here"
|
--- \\Other properties
|
--- -MEMF ... lMMu
|
--- commenttext: "hello here two"
|
--- \\Other properties
And the reference you should use is:
FirebaseDatabase.getInstance().getReference("comments")
.child(postid)
.child(commentid)
.child("comments"); //newly added
Upvotes: 1
Reputation: 1609
You want to get nested children count. So you need to iterate over children, and then for each children iterate over 2nd level comments. If you want to display count of every possible comment (doesn't matter if it's 1st or 2nd level), just do something like this (I am assuming that children of DataSnapshot also have children):
int commentNumber = 0;
for(int i=0; i<dataSnapshot.getChildrenCount(); i++) {
commentNumber += dataSnapshot.getChild(i).getChildrenCount() + 1; // add 2nd level child count and 1 for 1st level comment
}
Upvotes: 0