Java / Android - JSON Parsing to Create New Multi-Message Chat Only Results in One Message
<p>I'm attempting to parse a JSON Array into multiple messages - however in my current implementation each subsequent message in my Array overwrites the value of msg instead of creating a new message for each message value in the array. I understand I'll need to create a new message inside my for loop - but I can't seem to figure out how to do so programmatically.</p>

<p>Any suggestions are appreciated. </p>

<h2>JSON:</h2>

<pre><code>{"messages":[{"message":"User has joined.","type":"agent","created":"2016-11-26 12:56:02","name":"Test User"},{"message":"Hello World?","type":"agent","created":"2016-11-26 12:56:05","name":"Test User"}]}
</code></pre>

<h2>JAVA:</h2>

<pre><code>private void populateMessages(JSONObject msgObj) {
    for (int i = 0; i &lt; msgObj.length(); i++) {
        messages = ChatMessage.fromJson(msgObj);
        mAdapter.clear();
        mAdapter.addAll(messages);
        mListView.invalidateViews();
        mAdapter.notifyDataSetChanged();

    }
}



public static ChatMessage fromJson(JSONObject jsonObject) {
    ChatMessage msg = new ChatMessage();
    try {
        JSONArray items = jsonObject.getJSONArray("messages");
        for (int i = 0; i &lt; items.length(); i++) {
            JSONObject json_data = items.getJSONObject(i);
            if(json_data.getString("message") != null)
            msg.message = json_data.getString("message");
            if(json_data.getString("type") != null)
            msg.type = json_data.getString("type");
            if(json_data.getString("name") != null)
            msg.name = json_data.getString("name");

            if(msg.message.equals("Waiting for user.")){
                if(json_data.getString("created") != null)
                msg.created = convertInitalTime(json_data.getString("created"));
            }
            else{
                if(json_data.getString("created") != null)
                msg.created = convertTime(json_data.getString("created"));
            }

        }


    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
    return msg;
}
</code></pre>