title: Sent/Received messages gets repeated and order gets spoiled in peer-to-peer chat app in android using socket
body: <p>I am developing a peer-to-peer chat app using socket programming in android. The port number of self and IP address and port number of the other peer is given as input and connection is established through socket. I have implemented chat activity using a list-view with custom adapter when the chatting starts, after exchanging some messages, the old message reappears in the list-view and also the order of the sent and the received messages gets spoiled.</p>
  
  <p>I have printed the exchanged message in the logcat of android studio and seems that there is no issue. So what is the reason of the problem ? and What should be the solution?</p>
  
  <p>Here is the Chat Activity</p>
  
  <pre><code>  public class ChatActivity extends AppCompatActivity {
  
  String ipAddress,portNo;
  //public static String message="";
  EditText messageTextView;
  TextView responseTextView;
  static MessageAdapter mAdapter;
  ListView message_List;
  ArrayList&lt;Message&gt; messageArray;
  EditText portText;
  int myport;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      if(savedInstanceState != null){
          Log.d("STATE",savedInstanceState.toString());
      }
      setContentView(R.layout.activity_chat);
  
      Bundle bundle = getIntent().getExtras();
      if(bundle != null){
          String info = bundle.getString("ip&amp;port");
          String[] infos = info.split(" ");
          ipAddress = infos[0];
          portNo = infos[1];
          myport = Integer.parseInt(infos[2]);
          Log.d("info",ipAddress+" "+portNo+" "+myport);
      }
  
      message_List = (ListView) findViewById(R.id.message_list);
      messageArray = new ArrayList&lt;Message&gt;();
      mAdapter = new MessageAdapter(this, messageArray);
      message_List.setAdapter(mAdapter);
      messageTextView= (EditText) findViewById(R.id.messageEditText);
      //message = messageTextView.getText().toString();
      startServer();
  }
  
  void startServer(){
      Server s = new Server(message_List, messageArray, myport);
      s.start();
  }
  
  
  public void sendResponse(View view){
      Client c =new Client();
      c.execute();
  }
  public void setView(String s){
      String str=responseTextView.getText().toString();
      str=str+"\nReceived: "+s;
      responseTextView.setText(str);
  }
  public class Client extends AsyncTask&lt;Void,Void,String&gt; {
      String msg = messageTextView.getText().toString();;
      @Override
      protected String doInBackground(Void... voids) {
          try {
              String ipadd = ipAddress;
              int portr = Integer.parseInt(portNo);
              Socket clientSocket = new Socket(ipadd, portr);
              OutputStream outToServer =clientSocket.getOutputStream();
              PrintWriter output = new PrintWriter(outToServer);
              output.println(msg);
              output.flush();
              clientSocket.close();
          }
          catch (Exception e) {
              e.printStackTrace();
          }
          return msg;
      }
      protected void onPostExecute(String result) {
          messageArray.add(new Message("Sent: " + result, 0));
          message_List.setAdapter(mAdapter);
  
          Log.d("problem","Sent: " + result);
          for(Message mssg: messageArray){
              String sst = mssg.getMessage();
              //Log.d("problem","              "+sst);
          }
  
          messageTextView.setText("");
      }
  }
  
      }
  </code></pre>
  
  <p>Here is the code of the server</p>
  
  <pre><code>    public class Server extends Thread {
  ListView messageList;
  ArrayList&lt;Message&gt; messageArray;
  int port;
  public Server(ListView messageList, ArrayList&lt;Message&gt; messageArray, int 
      port) {
      this.messageArray = messageArray;
      this.messageList = messageList;
      this.port = port;
  }
  ServerSocket welcomeSocket=null;
  @Override
  public void run(){
      try{
          String sentence;
          welcomeSocket=new ServerSocket(port);
          while (true){
              Socket connectionSocket=welcomeSocket.accept();
              HandleClient c= new HandleClient();
              c.execute(connectionSocket);
          }
      }
      catch(Exception e){
          e.printStackTrace();
      }
  }
  public class HandleClient extends AsyncTask&lt;Socket,Void,String&gt;{
      String sentence;
      @Override
      protected String doInBackground(Socket... sockets) {
          try {
              BufferedReader input = new BufferedReader(new 
      InputStreamReader(sockets[0].getInputStream()));
              sentence = input.readLine();
          }
          catch(Exception e){
              e.printStackTrace();
          }
          return sentence ;
      }
      protected void onPostExecute(String result) {
          messageArray.add(new Message("Received: " + result, 1));
          messageList.setAdapter(mAdapter);
          Log.d("problem","Received: " + result);
          for(Message mssg: messageArray){
              String sst = mssg.getMessage();
              //Log.d("problem","              "+sst);
          }
      }
  }
      }
  </code></pre>
  
  <p>Here is my message adapter class</p>
  
  <pre><code>    public class MessageAdapter extends BaseAdapter{
  
  Context context;
  ArrayList&lt;Message&gt; arr = new ArrayList&lt;&gt;();
  
  public MessageAdapter(Context context,ArrayList&lt;Message&gt; arr) {
  
      this.context = context;
      this.arr = arr;
  
      for(Message mssg: arr){
          String sst = mssg.getMessage();
          Log.d("problem","              "+sst);
      }
  }
  
  @Override
  public int getCount() {
      return arr.size();
  }
  
  @Override
  public Object getItem(int i) {
      return arr.get(i);
  }
  
  @Override
  public long getItemId(int i) {
      return i;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent){
      //View listItemView=convertView;
      if(convertView==null){
          convertView= 
   LayoutInflater.from(context).inflate(R.layout.message_list,parent,false);
      }
      Message currentMessage= (Message) getItem(position);
      String message=currentMessage.getMessage();
  
      //Log.d("problem","    Current Message:    " + message);
  
      if(currentMessage.isSent()){
          TextView sent=(TextView) convertView.findViewById(R.id.list_sent);
          sent.setText(message);
          sent.setVisibility(View.VISIBLE);
          //sent.setVisibility(View.VISIBLE);
      }
      else{
          TextView received= (TextView) 
      convertView.findViewById(R.id.list_received);
          received.setText(message);
          received.setVisibility(View.VISIBLE);
      }
      return convertView;
  }
  }
  </code></pre>