android + json + how to display the extracted data in a list and when it clicked display the data in a single row? -
i creating simple android app data webserver in json type , display in listview when select row must display specified data in single row problem system show error selected item display in other activity.
can me ???
the error in jsonactivityhttpclient class in onpostexectute method, not take list "finalresult" neither:
new string[] { place_name_tag, latitude_tag,longitude_tag, postal_code_tag }
jsonparserhandler
package com.devleb.jsonparsingactivitydemo; import java.io.ioexception; import java.util.arraylist; import java.util.list; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.responsehandler; import org.apache.http.impl.client.basicresponsehandler; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import org.json.jsontokener; public class jsonparserhandler implements responsehandler<list<string>> { public static list<string> finalresult; public static final string place_name_tag = "placename"; public static final string longitude_tag = "lng"; public static final string latitude_tag = "lat"; private static final string admin_name_tag = "admincode3"; public static final string postal_code_tag = "postalcode"; private static final string postalcode = "postalcodes"; @override public list<string> handleresponse(httpresponse response) throws clientprotocolexception, ioexception { // todo auto-generated method stub finalresult = new arraylist<string>(); string jsonresponse = new basicresponsehandler() .handleresponse(response); try { jsonobject jsonobject = (jsonobject) new jsontokener(jsonresponse) .nextvalue(); jsonarray postalcodes = jsonobject.getjsonarray(postalcode); (int = 0; < postalcodes.length(); i++) { jsonobject postalcode = (jsonobject) postalcodes.get(i); string name = postalcode.getstring(place_name_tag); string lat = postalcode.getstring(latitude_tag); string lng = postalcode.getstring(longitude_tag); string postal = postalcode.getstring(postal_code_tag); list<string>tmlresult = new arraylist<string>(); tmlresult.add(name); tmlresult.add(lat); tmlresult.add(lng); tmlresult.add(postal); finalresult.addall(tmlresult); } } catch (jsonexception e) { e.printstacktrace(); } return finalresult; } }
jsonactivityhttpclient
package com.devleb.jsonparsingactivitydemo; import java.io.ioexception; import java.util.list; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httpget; import android.app.listactivity; import android.content.intent; import android.net.http.androidhttpclient; import android.os.asynctask; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; public class jsonactivityhttpclient extends listactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); new httpgettask().execute(); } private class httpgettask extends asynctask<void, void, list<string>> { private static final string user_name = "devleb"; private static final string url = "http://api.geonames.org/postalcodelookupjson?postalcode=6600&country=at&username=" + user_name; androidhttpclient mclient = androidhttpclient.newinstance(""); @override protected list<string> doinbackground(void... arg0) { // todo auto-generated method stub httpget request = new httpget(url); jsonparserhandler responsehandler = new jsonparserhandler(); try { return mclient.execute(request, responsehandler); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } protected void onpostexecute(void result) { // todo auto-generated method stub if (null != mclient) { mclient.close(); listadapter adapter = new simpleadapter( jsonactivityhttpclient.this, finalresult, r.layout.list_item, new string[] { place_name_tag, latitude_tag, longitude_tag, postal_code_tag }, new int[] { r.id.countryname, r.id.lat, r.id.lng, r.id.postalcode }); setlistadapter(adapter); } //setlistadapter(new arrayadapter<string>( // jsonactivityhttpclient.this, r.layout.list_item, result)); } } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.json_activity_http_client, menu); return true; } @override protected void onlistitemclick(listview l, view v, int position, long id) { // todo auto-generated method stub super.onlistitemclick(l, v, position, id); string place_name = ((textview) v.findviewbyid(r.id.countryname)).gettext().tostring(); string lat = ((textview) v.findviewbyid(r.id.lat)).gettext().tostring(); string lng = ((textview) v.findviewbyid(r.id.lng)).gettext().tostring(); string postal_code = ((textview) v.findviewbyid(r.id.postalcode)).gettext().tostring(); intent in = new intent(getbasecontext(), rowitem.class); in.putextra(place_name_tag, value) } }
mainactivity
package com.devleb.jsonparsingactivitydemo; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class mainactivity extends activity implements onclicklistener { final @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final button bntload = (button) findviewbyid(r.id.btnload); bntload.setonclicklistener(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public void onclick(view arg0) { // todo auto-generated method stub startactivity(new intent(getbasecontext(), jsonactivityhttpclient.class)); } }
rowitem
package com.devleb.jsonparsingactivitydemo; import android.os.bundle; import android.app.activity; import android.view.menu; public class rowitem extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.row_item); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.row_item, menu); return true; } }
i see 2 problem in code:
1- define asynctask class like
asynctask<void, void, list<string>>
but void on onpostexecute
method, need change
protected void onpostexecute(void result)
to
protected void onpostexecute(list<string> result)
2- try access finalresult on onpostexecute, bu define static on jsonparserhandler
if want access need :
jsonparserhandler.finalresult
but return on doinbackground
method can access with:
result
that in onpostexecute.
then need check result
equal null or not, because return null after catch
your onpostexecute must like:
protected void onpostexecute(list<string> result) { // todo auto-generated method stub if (null != mclient) { mclient.close(); if (result != null) { listadapter adapter = new simpleadapter( jsonactivityhttpclient.this, result, r.layout.list_item, new string[] { place_name_tag, latitude_tag, longitude_tag, postal_code_tag }, new int[] { r.id.countryname, r.id.lat, r.id.lng, r.id.postalcode }); setlistadapter(adapter); } else // thing want error happened }
Comments
Post a Comment