Saturday, April 20, 2013

[android help] Get the Json Array data in listview


In your for loop you are looping through all the values in the array and storing it in the same variable which replaces the value each time and contains only the last value.


Solution :



ArrayList> mp3List = new ArrayList>();
for(int i=0; i{
JSONObject obj = responseObject.getJSONObject(i);
String dmessage= obj.getString("dmessage");
String message= obj.getString("message");
String mp3= obj.getString("mp3");
String userMessageID= obj.getString("user_message_id");
String category= obj.getString("category");
String title= obj.getString("title");

//making use of obtained strings by adding it to some ArrayList to display in the ListView

HashMap map = new HashMap();

// adding each child node to HashMap key => value
map.put("dmessage", dmessage);
map.put("message", message);
map.put("mp3", mp3);
map.put("userMessageID", userMessageID);
map.put("category", category);
map.put("title", title);

// adding HashList to ArrayList
mp3List.add(map);
}

//you have all the data in mp3List. Display it in ListView
ListAdapter adapter = new SimpleAdapter(this, mp3List, custom_listitem_layout_id, new String[] { "dmessage", "message", "mp3", "userMessageID", "category", "title"}, new int[] { dmessage_textview_id, message_textview_id, mp3_textview_id, userMessageID_textview_id, category_textview_id,title_textview_id });
listview.setAdapter(adapter);


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...