Hello, the question is this: I have an ArrayAdapter with a list and EditText for data entry. When I enter the data and click the send button, the data is sent to the server and then output to the adapter. When I click on the button in the adapter, I immediately show the entered data, but as soon as new data comes from the server (that is, the same message that I entered into EditText), the adapter produces a duplicate of the last 2 items. That's what I tried to do ... here I add the data, and put the message ID = 0.

data = new ArrayList<>(); ResponseMsgArray rms = new ResponseMsgArray(); rms.setMsg(form_input_msg); rms.setNewMsg(getEndMsgId); rms.setMsg_id_us(Integer.toString(LV_USID)); rms.setMsg_id("0"); // тут sAdapter.addData(rms); 

And then, as soon as I got the answer, I look for ID = 0 and try to delete ..

 @Override public void response(ArrayList<ResponseMsgArray> response) { String id_msg; int size = response.size(); int count = response.size() - 1; if(count > 0) { getEndMsgId = response.get(count).getNewMsg(); } for(int i = 0; i < count; i++) { id_msg= response.get(i).getMsg_id(); Log.d(LOG_TAG, id_msg); } if(sAdapter == null) { sAdapter = new ChatMsgAdapter(this, response); listView.setAdapter(sAdapter); Log.d(LOG_TAG, "Array:: setAdapter" + getEndMsgId); } else { data = new ArrayList<>(); for(int i = 0; i < data.size(); i++) { String old_msg = data.get(i).getMsg_id(); if(old_msg.equals("0")) { sAdapter.remove(data.get(i)); Log.d(LOG_TAG, "Удалено" + data.size() + "->" + old_msg); } } sAdapter.setData(response); Log.d(LOG_TAG, "Array:: setData" + getEndMsgId); } } 

In this scenario, some sort of added data is sorted. For example, I add names (Vasya, Petya, Sasha), then the list was updated, the data was deleted .. the result (Petya, Vasya, Sasha), then the request to the server comes again, there is no new data, the usual notifyDataSetChanged is received, and the list gets its old view (Vasya, Peter, Sasha). Tell me how to properly implement what I'm trying? :)

    1 answer 1

    That was the way to do it.

     for(int _i = 0; _i < data.size(); _i++) { String msg = data.get(_i).getMsg(); String old_msg = data.get(_i).getMsg_id(); if (old_msg.equals("0")) { for (int i = 0; i < newData.size(); i++) { remove(data.get(_i)); Log.d(LOG_TAG, "Удалено" + i + "->" + msg); } } }