android - Update activity's TextView from EditText value within ListView's item -


so, have activity textview , listview custom baseadapter. activity looks this:

enter image description here

as can see, every item of list custom layout , basic idea is: every time numeric edittext within changes, "total" textview activity (which sum of prices of every product) must updated well.

i suppose must somehow done adapter class, don't know how it.

my activity file looks (it gets products data server via "getcollectionproducts" asynctask, set adapter):

public class productaisleactivity extends appcompatactivity implements view.onclicklistener{ listview productlist; button participate;  imagebutton search; edittext searchet; textview productstotal;  product[] colproducts; relativelayout collectionheader;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_product_aisle);     toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);     setsupportactionbar(toolbar);      getsupportactionbar().setdisplayhomeasupenabled(true);      /* ...        irrelevant code question      */      productstotal = (textview) findviewbyid(r.id.products_aisle_total);     productstotal.settext(             getresources().getstring(                     r.string.productstotal,                     string.valueof(0.00)             )     );      productlist = (listview) findviewbyid(r.id.products_aisle_list);     new getcollectionproducts().execute(); }  private class getcollectionproducts extends asynctask<void,void,jsonarray>{      @override     protected jsonarray doinbackground(void... voids) {         /* irrelevant code question */     }      @override     protected void onpostexecute(jsonarray jsonarray) {         /* irrelevant code question */                 productlist.setadapter(                         new collectionproductsadapter(                                 productaisleactivity.this,                                 colproducts                         )                 ); } } 

and adapter file looks follows:

public class collectionproductsadapter extends baseadapter { context context; productaisleactivity.product[] data; private static layoutinflater inflater = null;  public collectionproductsadapter(context context, productaisleactivity.product[] data) {     this.context = context;     this.data = data;     inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); }  @override public int getcount() {     return data.length; }  @override public object getitem(int i) {     return data[i]; }  @override public long getitemid(int i) {     return i; }  @override public view getview(int i, view view, viewgroup viewgroup) {     view v = view;     if (v == null) {         v = inflater.inflate(r.layout.product_row_layout, null);     }      productaisleactivity.product product = data[i];      /* ...        irrelevant code question      */      edittext productquantity = (edittext) v.findviewbyid(r.id.productquantity);     productquantity.settext("0");       return v; }  } 

i'm stuck @ point, appreciated.

first need listen changes in edittext can handle things dynamically without explicitly using submit button. can textwatcher.

productquanity.addtextchangedlistener(new textwatcher() {         private double originalcost = 0.0;          @override         public void beforetextchanged(charsequence s, int start, int count, int after) {             // before change text, set originalcost             // can know change after edit             originalcost = getcost(s.tostring());         }          @override         public void ontextchanged(charsequence s, int start, int before, int count) {             // don't need utilize method         }          @override         public void aftertextchanged(editable s) {              // after change has taken place in text,              // new cost , calculate difference              double newcost = getcost(s.tostring());              double changeincost = newcost - originalcost;         }          private double getcost(string input){             string count = input.tostring();             if(textutils.isempty(count))                 return 0.0;             else                 return (double) integer.parseint(count) * product.getprice();         }     }); 

now have change in cost, it? need notify activity have change. can observer, fine, fun let's use interface implement listener.

modify adapter class

public class collectionproductsadapter extends baseadapter {      public interface costchangedlistener{         void oncostchanged(double change);     }      context context;     productaisleactivity.product[] data;     private layoutinflater inflater = null; // shouldn't static     costchangedlistener listener;      public collectionproductsadapter(context context, productaisleactivity.product[] data, costchangedlistener listener) {         this.context = context;         this.data = data;         inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);         this.listener = listener;     }      // rest of code } 

now when update cost in our textwatcher can call

if(listener != null)     listener.oncostchanged(changeincost); 

last, make sure utilize correctly, need pass listener in our collectionproductsadapter constructor

productlist.setadapter(new collectionproductsadapter(             productaisleactivity.this, colproducts,             new costchangelistener(){                 @override                 public void oncostchanged(double change){                     double currenttotal = double.valueof(producttotal.gettext());                     double newtotal = currenttotal + change;                     producttotal.settext(string.valueof(newtotal));                 })); 

obviously may need tweak of match perfectly, , haven't tested things might off bit, should going in right direction. if have issue feel free comment , try through it.

notes

  1. do not keep static reference layout inflater
  2. it worth taking @ recyclerview or @ least viewholder pattern adapter

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -