android - populate a table layout with json -
i have android app should parse json output of php web service , display in table layout try following code no data showed after running app plz
public class createtable extends activity { tablelayout tl; tablerow tr; textview companytv,valuetv; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); tl = (tablelayout) findviewbyid(r.id.maintable); adddata(); } public void adddata() { try { string data; defaulthttpclient client = new defaulthttpclient(); httpget request = new httpget("http://192.168.1.10:89/.../z.php"); httpresponse response = client.execute(request); httpentity entity=response.getentity(); data=entityutils.tostring(entity); log.e("string", data); jsonarray json=new jsonarray(data); for(int i=0;i<json.length(); i++) { jsonobject obj=json.getjsonobject(i); string name=obj.getstring("part_name").tostring(); string id = obj.getstring("part_id").tostring() ; log.e("name", name); /** create tablerow dynamically **/ tr = new tablerow(this); tr.setlayoutparams(new layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); /** creating textview add row **/ companytv = new textview(this); companytv.settext(name); companytv.settextcolor(color.red); companytv.settypeface(typeface.default, typeface.bold); companytv.setlayoutparams(new layoutparams(layoutparams.fill_parent,layoutparams.wrap_content)); companytv.setpadding(5, 5, 5, 5); tr.addview(companytv); // adding textview tablerow. /** creating textview **/ valuetv = new textview(this); valuetv.settext(id); valuetv.settextcolor(color.green); valuetv.setlayoutparams(new layoutparams(layoutparams.fill_parent,layoutparams.wrap_content)); valuetv.setpadding(5, 5, 5, 5); valuetv.settypeface(typeface.default, typeface.bold); tr.addview(valuetv); // adding textview tablerow. // add tablerow tablelayout tl.addview(tr, new tablelayout.layoutparams( layoutparams.fill_parent, layoutparams.wrap_content)); }} catch(exception e){} }}
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" > <scrollview android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollbars="none"> <tablelayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchcolumns="0,1" android:id="@+id/maintable" > </tablelayout> </scrollview> </linearlayout>
first,in android development,the network operation should in thread not ui thread.
in place :
catch(exception e){}
you can print error check:
catch(exception e){ e.printstacktrace();}
Comments
Post a Comment