java - 昵称混合在评论部分 Firebase 数据库中
问题描述
所以我为我的应用做了一个简单的评论部分,现在的问题是,当新评论被写入时,他们混合了他们的时间戳,更重要的是他们的昵称(user_id 句柄名)。以下是我在两部手机之间制作的一些屏幕截图:
这是代码:
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);
}
}
最后是我的数据库的结构:
解决方案
根据您的第二个屏幕截图,我看到您希望按时间降序排列元素,但结果好坏参半。这是因为您试图根据user_id
属性而不是根据属性过滤您的评论date_created
。要根据时间戳属性降序过滤您的评论,请参阅此帖子中 Frank 的回答。
看到你的数据库,我很害怕你需要两个过滤器,但不幸的是 Firebase 实时数据库不支持对多个属性的查询,只支持对单个子属性的查询。因此,可以在此处找到可能的解决方案。
如果您考虑在某个时候尝试使用Cloud Firestore,请注意where()
允许更改多种方法。
推荐阅读
- ios - 如何使 iOS 辅助功能屏幕阅读器以编程方式阅读视图
- javascript - 如何在单击时使用输入来制作自定义选择器?
- c++ - MPI_Win_create 在原点的运行时间随着目标窗口大小的增加而增加
- jenkins - Jenkins 工作在尝试归档工件时如何忽略工作区?
- javascript - 如何解决 Fetch API js 中的 CORS 错误
- java - 根据 mouseEntered 和 mouseExited 更改组件的状态
- c++ - 无法反向操作。表示它无法将 void 转换为字符串但我的结果保存为字符串的错误。已显示代码以供参考
- azure - 通过 KeyVault 为 Azure Function 指定存储帐户
- concurrency - 计划的 Camel SQL 消费者,限制路由之间的并发
- redirect - wifi连接后将用户重定向到网页