I’m facing an issue with my comments functionality in my social networking app, my comments are replacing each other instead of placing themselves above and below each other, please help.
Here’s my code for CommentsActivity.java –
public class CommentsActivity extends AppCompatActivity {
private ImageButton PostCommentButton;
private EditText CommentInputText;
private RecyclerView CommentsList;
private String Post_key, current_user_id;
private DatabaseReference UsersRef, PostsRef;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
Post_key = getIntent().getExtras().get("PostKey").toString();
mAuth = FirebaseAuth.getInstance();
current_user_id = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(Post_key).child("comments");
CommentsList = findViewById(R.id.comments_list);
CommentsList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
CommentsList.setLayoutManager(linearLayoutManager);
CommentInputText = findViewById(R.id.comment_input);
PostCommentButton = findViewById(R.id.post_comment_btn);
PostCommentButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UsersRef.child(current_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists())
{
String userName = snapshot.child("username").getValue().toString();
ValidateComment(userName);
CommentInputText.setText("");
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
});
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Comments> options = new FirebaseRecyclerOptions.Builder<Comments>().setQuery(PostsRef, Comments.class).build();
FirebaseRecyclerAdapter<Comments,CommentsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Comments, CommentsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull CommentsViewHolder commentsViewHolder, int i, @NonNull Comments comments) {
commentsViewHolder.myUserName.setText(comments.getUsername());
commentsViewHolder.myComment.setText(comments.getComment());
commentsViewHolder.myDate.setText(comments.getDate());
commentsViewHolder.myTime.setText(comments.getTime());
}
@NonNull
@Override
public CommentsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.all_comments_layout, parent, false);
CommentsViewHolder viewHolder = new CommentsViewHolder(view);
return viewHolder;
}
};
CommentsList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
public static class CommentsViewHolder extends RecyclerView.ViewHolder
{
TextView myUserName, myComment, myDate, myTime;
public CommentsViewHolder(@NonNull View itemView) {
super(itemView);
myUserName = itemView.findViewById(R.id.comment_username);
myComment = itemView.findViewById(R.id.comment_text);
myDate = itemView.findViewById(R.id.comment_date);
myTime = itemView.findViewById(R.id.comment_time);
}
}
private void ValidateComment(String userName) {
String commentText = CommentInputText.getText().toString();
if(TextUtils.isEmpty(commentText))
{
Toast.makeText(this,"Please write text to comment...",Toast.LENGTH_LONG).show();
}
else
{
Calendar callForDate = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("dd-MMMM-yyyy");
final String saveCurrentDate = currentDate.format(callForDate.getTime());
Calendar callForTime = Calendar.getInstance();
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm");
final String saveCurrentTime = currentTime.format(callForDate.getTime());
final String RandomKey = current_user_id + saveCurrentDate + saveCurrentTime;
HashMap commentsMap = new HashMap();
commentsMap.put("uid",current_user_id);
commentsMap.put("comment",commentText);
commentsMap.put("date",saveCurrentDate);
commentsMap.put("time",saveCurrentTime);
commentsMap.put("username",userName);
PostsRef.child(RandomKey).updateChildren(commentsMap).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful())
{
Toast.makeText(CommentsActivity.this,"comment posted successfully....",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(CommentsActivity.this,"Error Occured, Try Again...",Toast.LENGTH_LONG).show();
}
}
});
}
}
}
Some of the comments are not being added to the database as well, and comments are replacing each other, please help