I am creating a social networking app using android studio and java, my problem is in comments functionality in the comments section of the app, whenever a user tries to post a comment his previous comments are replaced by new ones, please help!
Here’s the code where i think is the problem –
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comments);
Post_key = getIntent().getExtras().get("PostKey").toString();
PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(Post_key).child("comments").push();
String comment_push_id = PostsRef.getKey();
mAuth = FirebaseAuth.getInstance();
current_user_id = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
CommentsList = findViewById(R.id.comments_list);
CommentsList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
CommentsList.setLayoutManager(linearLayoutManager);
Here’s the related part of code which might be useful –
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) {
}
});
}
});
}
Here’s the part of code where a user inserts the comments into the firebase database –
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();
}
}
});
}
}
}
In the database also some comments are not getting stored, while only the replaced ones are getting stored, please help