0

I got confused how to separate JsonArray data. I want to separate the received chat messages so it appear one by one in each bubble not in becoming into one big bubble.
What am I supposed to do, to separate the data from each other?
Can anyone please help me? Thanking you in advance.

My json part of the code:

if(!content.equals("null")){
                        try{
                            JSONArray jArr = new JSONArray(content);

                              String messages="";
                              for(int i=0; i < jArr.length() ; i++){ 
                                  JSONObject jObj = jArr.getJSONObject(i);


                                     String message = jObj.getString("message");


                                     messages += message+"\n";

                              }

                              showMessage(messages, false);
                        }catch(JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    else{
                        Toast.makeText(ChatRoom.this, "Error", Toast.LENGTH_LONG).show();
                     }

Logcat:

06-22 09:15:20.486: D/ADBUG(519): content: [{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"test"},{"firstname":"teach","message":"testing chat"},{"firstname":"teach","message":"percobaan"},{"firstname":"teach","message":"per"},{"firstname":"teach","message":"tesssssssssss"},{"firstname":"teach","message":"ddffs"},

I want my data become like this:
enter image description here

7bluephoenix
  • 958
  • 7
  • 18
blackneko
  • 91
  • 1
  • 12

1 Answers1

1

Change one line Like this:

String messages="";
for(int i=0; i < jArr.length() ; i++){ 
     JSONObject jObj = jArr.getJSONObject(i);

     String message = jObj.getString("message");
     showMessage(messages, false);//move it to here but not outside of 'for'

}
JaveZh
  • 184
  • 3
  • JaveZh, may I ask you again, I have the problem when I want to show the friendlabel. I put `friendLabel.setText(name);` above the `showMessage` after I get the name etc i do that. that code works when print one name, but when try to print other name, it got force close. what am i suppose to do? can you help me please? i make new link for this topic [link]http://stackoverflow.com/questions/17249656/unable-to-show-friends-name-on-chat thank you very much – blackneko Jun 22 '13 at 10:50