简体   繁体   中英

Java / Android - JSON Parsing to Create New Multi-Message Chat Only Results in One Message

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.

Any suggestions are appreciated.

JSON:

{"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"}]}

JAVA:

private void populateMessages(JSONObject msgObj) {
    for (int i = 0; i < 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 < 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;
}
   private void populateMessages(JSONObject msgObj) {
        messages = ChatMessage.fromJson(msgObj);
        mAdapter.clear();
        mAdapter.addAll(messages);
        mListView.invalidateViews();
        mAdapter.notifyDataSetChanged();        
   }


public static ArrayList<ChatMessage> fromJson(JSONObject jsonObject) {
  ArrayList<ChatMessage> messages = new ArrayList<>();
  try {
    JSONArray items = jsonObject.getJSONArray("messages");
    for (int i = 0; i < items.length(); i++) {
       ChatMessage msg = new ChatMessage();
       ....
       ....
       messages.add(msg);
    }
  }

 return messages;
}

Move your new object in for loop and use ArrayList

Remove for loop in populteMessage

And in java prefer setter and getters

Thats all..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM