So I made a simple comment section for my app, the problem now is that when new comments are written they mix their timestamp and more importantly their nickname(user_id handlename). Here are some screenshots that I made between two phones:
Screenshot1 (test user) Screenshot2 my main phone(user2)
This is the code:
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);
}
}
And finally how my database is structured: