Android - ListView refresh/update from the Main Activity where it is called -
i have application lists values of product variant, unit, , quantity.
i have activity listview shown:
public class orderform extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_order_form); arraylist image_details = getlistdata(); final listview lv1 = (listview) findviewbyid(r.id.custom_list); lv1.setadapter(new customlistadapter(this, image_details)); } private arraylist getlistdata() { arraylist results = new arraylist(); orderdetailsclass orderdata = new orderdetailsclass(); return results; } public void onadditem(view view){ arraylist results = new arraylist(); orderdetailsclass orderdata = new orderdetailsclass(); orderdata = new orderdetailsclass(); orderdata.setvariants("flavored"); orderdata.setunit("cans"); orderdata.setquantity(1); results.add(orderdata); //selectvariant(); //selectunit(); //selectquantity(); } }
this customadapter
public class customlistadapter extends baseadapter { private arraylist<orderdetailsclass> listdata; private layoutinflater layoutinflater; public customlistadapter(context context, arraylist<orderdetailsclass> listdata) { this.listdata = listdata; layoutinflater = layoutinflater.from(context); } @override public int getcount() { return listdata.size(); } @override public object getitem(int position) { return listdata.get(position); } @override public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = layoutinflater.inflate(r.layout.list_row_layout, null); holder = new viewholder(); holder.variantview = (textview) convertview.findviewbyid(r.id.variant); holder.unitview = (textview) convertview.findviewbyid(r.id.unit); holder.quantityview = (textview) convertview.findviewbyid(r.id.quantity); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.variantview.settext(listdata.get(position).getvariants()); holder.unitview.settext("by, " + listdata.get(position).getunit()); holder.quantityview.settext(string.valueof(listdata.get(position).getquantity())); return convertview; } static class viewholder { textview variantview; textview unitview; textview quantityview; } public void updateresults(arraylist<orderdetailsclass> results) { listdata = results; //triggers list update notifydatasetchanged(); } }
this list_row_layout
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- android:minheight="50dp" android:orientation="horizontal" android:padding="5dip" --> <textview android:id="@+id/variant" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:text="variant" /> <textview android:id="@+id/unit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:text="unit" /> <textview android:id="@+id/quantity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:text="quantity" /> </relativelayout>
this activity_order_form
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <listview android:id="@+id/custom_list" android:layout_width="fill_parent" android:layout_height="386dp" android:dividerheight="1dp" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:onclick="onadditem" android:text="add" /> </relativelayout>
what want when press onadditem
function, after data has been added, listview refresh , display changes. far, i've seen notifydatasetchanged()
being used in baseadapter
class. i've tried seems notifydatasetchanged()
should called within baseadapter
, want able refresh orderform
activity.
any ideas?
thanks.
keep reference customadapter in orderform activity:
public class orderform extends activity { customlistadapter customlistadapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_order_form); arraylist image_details = getlistdata(); final listview lv1 = (listview) findviewbyid(r.id.custom_list); customlistadapter = new customlistadapter(this, image_details); lv1.setadapter(customlistadapter); }
then can call customadapter.notifydatasetchanged()
. public method on adapter can call activity fine.
Comments
Post a Comment