0

I'm using socket.io for my chat app. I have an ArrayList which contains last message, username, time. Whenever a new message arrives in JSON format then it should check if JSON contained username is present in ArrayList or not. If present, then updates the ArrayList otherwise add in ArrayList. Here is my code:-

private Emitter.Listener handle1 = new Emitter.Listener() {
    @Override
    public void call(final Object... args) {
        ChatLists.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                JSONObject data = (JSONObject)args[0];
                try {
                    String sendername = data.getString("sender");
                    String lastMessage = data.getString("message");
                    String profileImage = data.getString("Profile");
                    String token = data.getString("fb_token");
                    chat_list chat_list = new chat_list(sendername,
                            profileImage, lastMessage, "0", "", "dummy", token);


                    if (chat_lists.size()==0){
                        chat_lists.add(chat_list);
                    }else {
                        for (int i=0;i<chat_lists.size();i++){
                            if (chat_lists.get(i).getContactname().equals(sendername)){
                                chat_lists.set(i,chat_list);
                            }else {
                                chat_lists.add(chat_list)
                            }
                        }

                    }
                    contactlistAdapter = new ContactlistAdapter(chat_lists);
                    recyclerView.setAdapter(contactlistAdapter);
                    contactlistAdapter.notifyDataSetChanged();


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
            }
        });
    }
};
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126

4 Answers4

0

Well, you can use contains() & set() methods of ArrayList in a logical way to solve your problem like below:-

if(chat_lists.contains(username)) 
 chat_lists.set(indexOf(username), new_username);

else chat_lists.add(new_username);
Abhinav
  • 530
  • 8
  • 21
0

Try it:

if(chat_lists.contains(chat_list)){
  chat_lists.remove(chat_list);
  chat_lists.add(chat_list);
} else {
  chat_lists.add(chat_list);
}
Shiva Kumar N
  • 371
  • 3
  • 11
0

Read about architecture patterns, for example, MVP. You need to store your messages somethere (in Model) and update view relative to data.

Also read about RecyclerView, cause of ListView is a little bit deprecated

stfbee
  • 431
  • 5
  • 10
-1
if (chat_lists.get(i).getContactname().equals(sendername)){

above statement has problem them. It's not getting under your if condition and following the chat_lists.add(chat_list) statement.

Instead equals use ignoreCasequals. If still wont it solve your problem please use debug mode or logs check chat_lists.get(i).getContactname() and sendername same or not.

Sush
  • 3,864
  • 2
  • 17
  • 35