评论的名称和时间戳记Firebase数据库的顺序不正确

时间:2018-12-05 14:51:26

标签: android firebase firebase-realtime-database server

我的应用程序的注释部分无法正常运行,孩子们正在通过他们的姓名和时间戳(其中的内容)混在一起其中的顺序是正确的顺序,这是Pastebin链接中的我的所有代码如有必要以查看完整的代码),我提供了,我认为问题出在这里。

1)CommentAdapter.java活动https://pastebin.com/m7LHDUDF) 注意以下代码块:

private void getHandleName(final ViewHolder viewHolder) {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Log.d(TAG, "getHandleName: checking comment userID" + viewHolder.comment.getUser_id());
    Query query;
    query = reference
            .child("data")
            .child("-Kxzyb5JsUPhsMQAb84X")
            .child("users")
            .orderByChild("user_id")
            .equalTo(viewHolder.comment.getUser_id());

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                viewHolder.handleName.setText(singleSnapshot.getValue(User.class).getHandlename());
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

2)CommentRecylerViewAdapter https://pastebin.com/Tb7L9EVD

 private void getHandleName(final CommentViewHolder viewHolder, Comment comment) {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    Log.d(TAG, "getHandleName: checking comment userID" + comment.getUser_id());
    Query query = reference
            .child("data")
            .child("-Kxzyb5JsUPhsMQAb84X")
            .child("users")
            .orderByChild("user_id")
            .equalTo(comment.getUser_id());


    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
                viewHolder.handleName.setText(singleSnapshot.getValue(User.class).getHandlename());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

3)大量用于添加评论的代码: `private void addComment(){

    if (commentText.getText().toString().isEmpty()) {
        Toast.makeText(ViewPostActivity.this, "Please enter your comment", Toast.LENGTH_SHORT).show();
    } else {
        String currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        String commentID = reference.push().getKey();

        Comment comment = new Comment();
        comment.setCaption(commentText.getText().toString());
        comment.setDate_created(System.currentTimeMillis());
        comment.setUser_id(currentUserID);

        reference.child("data").child("-Kxzyb5JsUPhsMQAb84X").child("comments").child(postID).child(commentID).setValue(comment);
        setNumComment();
        setNumPointCurrentUser();
        setNumPointUser();
        setNumPointPost();
    }
}`

4)**最后,我认为可以的c omment模型Activity **(https://pastebin.com/VaYV3Gv3

这是我的数据库的屏幕截图:

  1. -Kxzyb5JsUPhsMQAb84X是我数据库的根
  2. 评论是每个帖子存储所有评论的地方
  3. -LSv6lXZml-Cf0GX3i5q随机生成的子项,用于存储其他内容,例如时间戳,用户ID和内容

Database

这是我手机的屏幕截图,显示了评论的不正确顺序:

Phone Screenshot

0 个答案:

没有答案