所以我为我的应用程序创建了一个简单的注释部分,现在的问题是,在编写新注释时,它们混合了时间戳,更重要的是混合了昵称(user_id句柄名称)。这是我在两部手机之间制作的一些屏幕截图:
Screenshot1 (test user) Screenshot2 my main phone(user2)
这是代码:
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);
}
}
最后是我的数据库的结构:
答案 0 :(得分:0)
根据您的第二个screenshot,我看到您想对元素按时间降序进行排序,但结果却很复杂。这是因为您尝试根据user_id
属性过滤评论,而根据date_created
属性不过滤评论。要根据时间戳属性降序过滤您的评论,请参见post的弗兰克回答。
看到您的数据库,我很困惑,您将同时需要两个过滤器,但是很遗憾,Firebase实时数据库不支持对多个属性的查询,仅支持对单个子属性的查询。因此,可以找到 here 的解决方案。
如果您考虑在某个时候尝试使用Cloud Firestore,请注意,可以改变多个where()
方法。