• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android Json Example

Baggio1990

Lurker
Dec 11, 2013
3
0
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
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones