android - output of my json response is displaying last item in the database -
i trying display content of database in tablelayout, have fetch data database, , set displayed in android app, when data displays, overwrite data fetch earlier , show last item.
this except form code think got code wrong
@override protected void onpostexecute(string result) { /* textview textview= (textview)findviewbyid(r.id.textview); textview.settext(result);*/ json_string = result; try { jsonobject = new jsonobject(json_string); jsonarray = jsonobject.getjsonarray("server_response"); int count = 0; string id, inv_id, refnumber, description, details, qty, unit_price, amount; double subtotal = 00.00; int sn = 1; double damount; while (count < jsonarray.length()) { jsonobject jo = jsonarray.getjsonobject(count); description = jo.getstring("description"); qty = jo.getstring("qty"); unit_price = jo.getstring("unit_price"); amount = jo.getstring("amount"); txtdescription.settext(description); txtqty.settext(qty); txtunit_price.settext(unit_price); txtamount.settext(amount); txtsn.settext(string.valueof(sn)); count++; sn++; try { subtotal = subtotal + double.valueof(amount); // damount = double.valueof(amount); } catch (numberformatexception e) { subtotal = 00.00; } } txtsubtotal.settext(string.valueof(subtotal)); double vat = 0.05*(subtotal); txtvat.settext(string.valueof(vat)); double total = vat + subtotal; txttotal.settext(string.valueof(total)); } catch (jsonexception e) { e.printstacktrace(); } }
my xml layout follows
<?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ng.proartisan.invoiceapp.mainactivity"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <tablelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <tablerow> <textview android:text="s/n" android:background="@drawable/cellborder" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:textstyle="bold" /> <textview android:text="description" android:background="@drawable/cellborder" android:padding="5dp" android:layout_weight="4" android:gravity="center" android:textstyle="bold" /> <textview android:text="qty" android:background="@drawable/cellborder" android:layout_weight="1" android:padding="5dp" android:gravity="center" android:textstyle="bold" /> <textview android:text="unit price" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" android:textstyle="bold" /> <textview android:text="amount" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" android:textstyle="bold" /> </tablerow> <tablerow> <textview android:id="@+id/sn" android:text="s/n" android:background="@drawable/cellborder" android:layout_weight="1" android:gravity="center" android:padding="5dp" /> <textview android:id="@+id/description" android:text="description" android:background="@drawable/cellborder" android:padding="5dp" android:layout_weight="4" android:gravity="center" /> <textview android:id="@+id/qty" android:text="qty" android:background="@drawable/cellborder" android:layout_weight="1" android:padding="5dp" android:gravity="center" /> <textview android:id="@+id/unit_price" android:text="unit price" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> <textview android:id="@+id/amount" android:text="amount" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> </tablerow> <tablerow> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:gravity="center" android:padding="5dp" /> <textview android:text="subtotal" android:background="@drawable/cellborder" android:padding="5dp" android:layout_weight="4" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:padding="5dp" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> <textview android:id="@+id/subtotal" android:text="" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> </tablerow> <tablerow> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:gravity="center" android:padding="5dp" /> <textview android:text="vat" android:background="@drawable/cellborder" android:padding="5dp" android:layout_weight="4" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:padding="5dp" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> <textview android:id="@+id/vat" android:text="" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> </tablerow> <tablerow> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:gravity="center" android:padding="5dp" /> <textview android:text="total" android:background="@drawable/cellborder" android:padding="5dp" android:layout_weight="4" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:padding="5dp" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> <textview android:id="@+id/total" android:text="" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> </tablerow> </tablelayout> </linearlayout> </scrollview>
please help
you setting data same view in loop, other way solve problem create new layout , add layout example: xml file layout_item_description.xml
<tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <tablerow> <textview android:id="@+id/tv_sn" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:text="s/n" android:textstyle="bold" /> <textview android:id="@+id/tv_description" android:layout_weight="4" android:gravity="center" android:padding="5dp" android:text="description" android:textstyle="bold" /> <textview android:id="@+id/tv_qty" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:text="qty" android:textstyle="bold" /> <textview android:id="@+id/tv_unit_price" android:layout_weight="2" android:gravity="center" android:padding="5dp" android:text="unit price" android:textstyle="bold" /> <textview android:id="@+id/tv_amount" android:layout_weight="2" android:gravity="center" android:padding="5dp" android:text="amount" android:textstyle="bold" /> </tablerow> </tablelayout>
add item view layout. xml layout file:
<?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ng.proartisan.invoiceapp.mainactivity"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <tablelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <tablerow> <textview android:text="s/n" android:background="@drawable/cellborder" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:textstyle="bold" /> <textview android:text="description" android:background="@drawable/cellborder" android:padding="5dp" android:layout_weight="4" android:gravity="center" android:textstyle="bold" /> <textview android:text="qty" android:background="@drawable/cellborder" android:layout_weight="1" android:padding="5dp" android:gravity="center" android:textstyle="bold" /> <textview android:text="unit price" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" android:textstyle="bold" /> <textview android:text="amount" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" android:textstyle="bold" /> </tablerow> </tablelayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/ll_data_description"/> <tablelayout android:layout_width="match_parent" android:layout_height="wrap_content"> <tablerow> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:gravity="center" android:padding="5dp" /> <textview android:text="subtotal" android:background="@drawable/cellborder" android:padding="5dp" android:layout_weight="4" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:padding="5dp" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> <textview android:id="@+id/subtotal" android:text="" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> </tablerow> <tablerow> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:gravity="center" android:padding="5dp" /> <textview android:text="vat" android:background="@drawable/cellborder" android:padding="5dp" android:layout_weight="4" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:padding="5dp" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> <textview android:id="@+id/vat" android:text="" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> </tablerow> <tablerow> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:gravity="center" android:padding="5dp" /> <textview android:text="total" android:background="@drawable/cellborder" android:padding="5dp" android:layout_weight="4" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="1" android:padding="5dp" android:gravity="center" /> <textview android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> <textview android:id="@+id/total" android:text="" android:background="@drawable/cellborder" android:layout_weight="2" android:padding="5dp" android:gravity="center" /> </tablerow> </tablelayout> </linearlayout> </scrollview>
add linearlayout ll_data_description = (linearlayout)findviewbyid(r.id.ll_data_description);
@override protected void onpostexecute(string result) { /* textview textview= (textview)findviewbyid(r.id.textview); textview.settext(result);*/ json_string = result; try { jsonobject = new jsonobject(json_string); jsonarray = jsonobject.getjsonarray("server_response"); int count = 0; string id, inv_id, refnumber, description, details, qty, unit_price, amount; double subtotal = 00.00; int sn = 1; double damount; while (count < jsonarray.length()) { jsonobject jo = jsonarray.getjsonobject(count); view layout_item_description = getlayoutinflater().inflate(r.layout.layout_item_description, null); ((textview) layout_item_description.findviewbyid(r.id.tv_sn)).settext(sn+""); description = jo.getstring("description"); qty = jo.getstring("qty"); unit_price = jo.getstring("unit_price"); amount = jo.getstring("amount"); ((textview) layout_item_description.findviewbyid(r.id.tv_description)).settext(sn+""); ((textview) layout_item_description.findviewbyid(r.id.tv_qty)).settext(sn+""); ((textview) layout_item_description.findviewbyid(r.id.tv_unit_price)).settext(sn+""); ((textview) layout_item_description.findviewbyid(r.id.tv_amount)).settext(sn+""); count++; sn++; ll_data_description.addview(layout_item_description); try { subtotal = subtotal + double.valueof(amount); // damount = double.valueof(amount); } catch (numberformatexception e) { subtotal = 00.00; } } txtsubtotal.settext(string.valueof(subtotal)); double vat = 0.05*(subtotal); txtvat.settext(string.valueof(vat)); double total = vat + subtotal; txttotal.settext(string.valueof(total)); } catch (jsonexception e) { e.printstacktrace(); } }
Comments
Post a Comment