When you open an Activity, a get-request (getComment) is sent to the server and a list with comments is displayed in recyclerview. When a button is pressed, a post request (addComment) is sent to the server, after which it is necessary that the list with comments is updated, but when using recyclerView.getAdapter().notifyAll(); or recyclerView.getAdapter().notify(); java.lang.IllegalMonitorStateException: object not locked by thread before notify(). error occurs java.lang.IllegalMonitorStateException: object not locked by thread before notify(). How to update recyclerview after adding a comment?
public class Activity extends AppCompatActivity { RecyclerView recyclerView; List<Object> items; public static RequestInterface requestInterface; Button btnAddComment; EditText etComment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); etComment = (EditText) findViewById(R.id.et_comment); btnAddComment = (Button) findViewById(R.id.btn_add_comment); requestInterface = Controller.getApi(); items = new ArrayList<>(); recyclerView = (RecyclerView) findViewById(R.id.recycle_view); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); MyAdapter adapter = new MyAdapter(this, items); recyclerView.setAdapter(adapter); requestInterface.getComment(id).enqueue(new Callback <List<Comment>>() { @Override public void onResponse(Call <List<Comment>> call, Response <List<Comment>> response) { items.addAll(response.body()); recyclerView.getAdapter().notifyDataSetChanged(); } @Override public void onFailure(Call <List<Comment>> call, Throwable t) { } }); btnAddComment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AddComment addComment = new AddComment(); addComment.setText(etComment.getText().toString()); requestInterface.addComment(addComment).enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { } @Override public void onFailure(Call<Void> call, Throwable t) { } recyclerView.getAdapter().notifyAll(); } }); } }