Apps Android Json Example

hello.i try to parse json and show item to listview.but i want to show all items on button click , but when i clicked button only show last item on listview
this is a my my code
[HIGH]private class LoadDataToServer extends
AsyncTask<Void, Integer, ArrayList<HashMap<String, String>>> {

@Override
protected void onPreExecute() {
super.onPreExecute();
pd.show();

}

@Override
protected ArrayList<HashMap<String, String>> doInBackground(
Void... params) {

jsonparser = new JSONParser();

JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
try {

jsonarray = jsonobject.getJSONArray("data");

for (int i = 0; i < jsonarray.length(); i++) {

jsonobject = jsonarray.getJSONObject(i);

HashMap<String, String> map = new HashMap<String, String>();

map.put("journal", jsonobject.getString(KEY_journal));
map.put("image", jsonobject.getString(KEY_image));
map.put("title", jsonobject.getString(KEY_title));
map.put("description",jsonobject.getString(KEY_description));

map.put("pubDate", jsonobject.getString(KEY_image));

contents = new ArrayList<Content>();

Content cont = new Content(jsonobject.getString("journal"),
jsonobject.getString("image"),jsonobject.getString("title")
,jsonobject.getString("pubDate")
,jsonobject.getString("description"));


contents.add(cont);

itemList.add(map);

}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return itemList;
}

@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
if (pd.isShowing()) {
pd.dismiss();
try {

adapter = new LazyAdapter(MainActivity.this, itemList);
list.setAdapter(adapter);

} catch (NullPointerException e) {
e.printStackTrace();
}

}

}
}[/HIGH]
It all components function display
[HIGH]public void showAllChannels() {
itemList.clear();
adapter.notifyDataSetChanged();

for (int i = 0; i < contents.size(); i++) {

HashMap<String, String> map = new HashMap<String, String>();

map.put(KEY_journal, contents.get(i).journal);
map.put(KEY_image, contents.get(i).image);
map.put(KEY_title, contents.get(i).title);

// map.put(KEY_pubDate, contents.get(i).pubDate);
map.put(KEY_description, contents.get(i).description);

itemList.add(map);
}

adapter.notifyDataSetChanged();
}[/HIGH]
What is the problem? If anyone can help, please help me
thanks
 

GeorgeN

Well-Known Member
I guess because every time you loop around in doInBackground you construct a new contents object and then add 1 item to it. When you call showAllChannels it will only loop once since contents.size()==1.
 
Top