chohyunwook chohyunwook - 3 months ago 11
Android Question

showing JSON parser listview on android

In my server , showing text this.

[{"name":"AAA", "age":"1"},
{"name":"BBB", "age":"2"},
{"name":"CCC", "age":"3"}]


and I want see this text my phone.

so, I use asynctask on android

String strData = "";

.
.
.
@Override
protected void onPostExecute(String result) { //result = show server text
super.onPostExecute(result);

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
try {
JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
strData += object.getString("name")
+ object.getInt("age");

adapter.add(strData);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}


this result my phone

AAA 1
----------------
AAA 1
BBB 2
----------------


but I want show

AAA 1
-----------
BBB 2


this.

perhaps,
for (int i = 0; i < jsonArray.length(); i++) {
here problem?

please advice for me
thanks.

Answer

It's so easy, see this:

for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject object = jsonArray.getJSONObject(i);
        strData += object.getString("name")
                +  object.getInt("age");

        adapter.add(strData);
        strData = "";
   }
Comments