title: ListView chat message for Firebase
body: <p>I have created an app which involves chatting with other people, and to make the chat activity more pleasing to the eye I created a <code>ListView</code> to change the UI of each message. When I send a message the first message appears and displays correctly but if I send another message it overrides the previous message in the listView instead of just adding another value to the list view, here's my code:</p>
  
  <p>ChatActivty:</p>
  
  <pre><code>private void append_chat_conversation(DataSnapshot dataSnapshot) {
          for (DataSnapshot ds : dataSnapshot.getChildren()) {
              Log.e("Chat details: ", String.valueOf(ds));
              chat_username = dataSnapshot.child("username").getValue(String.class);
              chat_msg = dataSnapshot.child("message").getValue(String.class);
  
              Chat chat = new Chat(chat_username, chat_msg);
              ArrayList&lt;Chat&gt; chatList = new ArrayList&lt;&gt;();
              chatList.add(chat);
  
              chatListAdapter adapter = new chatListAdapter(this, R.layout.chat_message, chatList);
              mListView.setAdapter(adapter);
              Log.e("Chat username: ", "" + chat_username);
              Log.e("Chat message: ", "" + chat_msg);
  
          }
  }
  </code></pre>
  
  <p>Chat List adapter:</p>
  
  <pre><code>public class chatListAdapter extends ArrayAdapter&lt;Chat&gt; {
  
      private Context mContext;
      int mResource;
      TextView usernameTV, messageTV;
  
      public chatListAdapter(Context context, int resource, ArrayList&lt;Chat&gt; objects) {
          super(context, resource, objects);
          mContext = context;
          mResource = resource;
  
      }
  
      @NonNull
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          String username = getItem(position).getUsername();
          String message = getItem(position).getMessage();
  
          Chat chat = new Chat(username, message);
  
          LayoutInflater inflater = LayoutInflater.from(mContext);
          convertView = inflater.inflate(mResource, parent, false);
  
          usernameTV = convertView.findViewById(R.id.username_chat);
          messageTV = convertView.findViewById(R.id.message_chat);
  
          usernameTV.setText(username);
          messageTV.setText(message);
  
          return convertView;
      }
  }
  </code></pre>
  
  <p>I won't add the other bits of code (the getter and setters or the actual layout of the individual messages) because I don't think that's a problem. Let me know if you need anything else, I'm sure I'm doing something stupid here to make this happen,</p>
  
  <p>Thanks, Nathan</p>
  
  <p>EDIT:</p>
  
  <p>I finally found out how to add to the listview without it overriding with this code:</p>
  
  <pre><code>private void append_chat_conversation(DataSnapshot dataSnapshot) {
          for (DataSnapshot ds : dataSnapshot.getChildren()) {
              Log.e("Chat details: ", String.valueOf(ds));
              chat_username = dataSnapshot.child("username").getValue(String.class);
              chat_msg = dataSnapshot.child("message").getValue(String.class);
  
              Chat chat = new Chat(chat_username, chat_msg);
              chatList.add(chat);
              Log.e("Chat username: ", "" + chat_username);
              Log.e("Chat message: ", "" + chat_msg);
          }
          adapter.notifyDataSetChanged();
      }
  </code></pre>
  
  <p>with the array list and adpater being a public variable at the top, however it still shows the message twice. I still need to find a fix for that. Thanks for the help</p>