首页 > 解决方案 > 昵称混合在评论部分 Firebase 数据库中

问题描述

所以我为我的应用做了一个简单的评论部分,现在的问题是,当新评论被写入时,他们混合了他们的时间戳,更重要的是他们的昵称(user_id 句柄名)。以下是我在两部手机之间制作的一些屏幕截图:

截图1(测试用户) 截图2我的主要手机(用户2)

这是代码:

 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")
            .child("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());

 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();
    }
}


            }
        }
Timestamp code:
private void getTimeDifference() {

    long timeCurrent = System.currentTimeMillis() / 1000;
    long timePost = postTime / 1000;
    long timeDifference = timeCurrent - timePost;

    if (timeDifference < 60) {
        String time = timeDifference + "s";
        timestamp.setText(time);
    } else if (timeDifference < 3600) {
        String time = timeDifference / 60 + "m";
        timestamp.setText(time);
    } else if (timeDifference < 86400) {
        String time = timeDifference / 3600 + "h";
        timestamp.setText(time);
    } else if (timeDifference < 604800) {
        String time = timeDifference / 86400 + "d";
        timestamp.setText(time);
    } else if (timeDifference < 2419200) {
        String time = timeDifference / 604800 + "w";
        timestamp.setText(time);
    } else if (timeDifference < 29030400) {
        String time = timeDifference / 2419200 + "M";
        timestamp.setText(time);

    } else {
        String result = (String) DateUtils.getRelativeTimeSpanString(System.currentTimeMillis(), postTime, 0);
        String time = result.replace("In ", "");
        timestamp.setText(time);
    }
}

最后是我的数据库的结构:

在此处输入图像描述

标签: javaandroidfirebasefirebase-realtime-database

解决方案


根据您的第二个屏幕截图,我看到您希望按时间降序排列元素,但结果好坏参半。这是因为您试图根据user_id属性而不是根据属性过滤您的评论date_created。要根据时间戳属性降序过滤您的评论,请参阅此帖子中 Frank 的回答。

看到你的数据库,我很害怕你需要两个过滤器,但不幸的是 Firebase 实时数据库不支持对多个属性的查询,只支持对单个子属性的查询。因此,可以在此处找到可能的解决方案。

如果您考虑在某个时候尝试使用Cloud Firestore,请注意where()允许更改多种方法。


推荐阅读