0

При открытии Activity на сервер отправляется get-запрос(getComment) и в recyclerview выводится список с комментариями. При нажатии кнопки на сервер посылается post-запрос(addComment), после чего нужно, чтобы список с комментариями обновился, но при использовании recyclerView.getAdapter().notifyAll(); или recyclerView.getAdapter().notify(); выпадает ошибка java.lang.IllegalMonitorStateException: object not locked by thread before notify(). Как правильно обновить recyclerview после добавления комментария?

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();
 }
        });
    }
}

1 ответ 1

3

Во первых, для добавления нового итема, его нужно добавить в коллекцию, связанную с адаптером:

items.add(addComment);

Затем нужно обновить адаптер:

recyclerView.getAdapter().notifyItemInserted(items.size() - 1);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.