I'm making a CommentFragment. I have a RecyclerView to list comments and a Edittext to write comment. However when I add a comment it's sent to server but RecyclerView isn't updated. I use notifydatasetchanged
to update. Code:
private void getComments(){
Call<List<CommentsModel>> call=restApiClass.getComments(post_id);
call.enqueue(new Callback<List<CommentsModel>>() {
@Override
public void onResponse(Call<List<CommentsModel>> call, Response<List<CommentsModel>> response) {
if(response.isSuccessful()){
list=response.body();
if(commentsAdapter==null) {
commentsAdapter = new CommentsAdapter(list, getContext());
}
else{
commentsAdapter.notifyDataSetChanged();
}
recyclerView.setAdapter(commentsAdapter);
}
}
@Override
public void onFailure(Call<List<CommentsModel>> call, Throwable t) {
}
});
}
I call this method when I click to sendCommentTextView:
sendCommentTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//............send comment codes.........
getComments();
}
});