如何获取下面指示的节点的子节点数量?

huangapple go评论69阅读模式
英文:

How to get the children count of the node indicated below?

问题

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.

这段代码是我正在使用的。

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("查看全部 " + numberOfComments + " 条评论");
            }else if (numberOfComments == 0){
                commentsonComment.setText("暂无评论");
            }else {
                commentsonComment.setText("查看 " + numberOfComments + " 条评论");
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

请注意,我将代码中的HTML转义字符(如")替换为普通的双引号(")以确保代码能够正常运行。

英文:

如何获取下面指示的节点的子节点数量?

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) {

        }
    });
}

答案1

得分: 1

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
英文:

> 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

答案2

得分: 0

你想要获取嵌套子级的数量。因此,您需要迭代子级,然后对每个子级迭代第二级评论。如果您想要显示每个可能评论的数量(无论是第一级还是第二级评论),只需像这样做(假设DataSnapshot的子级也有子级):

int commentNumber = 0;
for(int i=0; i<dataSnapshot.getChildrenCount(); i++) {
    commentNumber += dataSnapshot.getChild(i).getChildrenCount() + 1; // 添加第二级子级数量和1个第一级评论
}
英文:

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&lt;dataSnapshot.getChildrenCount(); i++) {
    commentNumber += dataSnapshot.getChild(i).getChildrenCount() + 1; // add 2nd level child count and 1 for 1st level comment
}

</details>



huangapple
  • 本文由 发表于 2020年8月10日 15:54:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63336200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定