android - how can i generate the total amount automatically? -
i new android , trying generate total amount automatically after user input quantity in buying page. first product price activity , make edit text quantity. after page auto show total amount. has experience this.
my code:
public class pagebuy extends activity { textview lblpid; textview lblname; textview lblamount; edittext quantity; integer total_amount; private static final string key_error_msg = "error_msg"; private static final string tag_name = "product_name"; private static final string tag_pid = "product_id"; private static final string tag_amount = "amount"; private static final string key_quantity = "quantity"; private static final string key_uid = "uid"; private static final string key_created_at = "created_at"; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.pagebuy); intent gg = getintent(); string product_id = gg.getstringextra(tag_pid); string product_name = gg.getstringextra(tag_name); string amount = gg.getstringextra(tag_amount); lblpid = (textview) findviewbyid(r.id.product_id_label); lblname = (textview) findviewbyid(r.id.product_name_label); lblamount = (textview) findviewbyid(r.id.amount_label); lblpid.settext(product_id); lblname.settext(product_name); lblamount.settext(amount); quantity = (edittext) findviewbyid(r.id.quantity); string quan = quantity.gettext().tostring().trim(); system.out.println(quan +"short"); //userfunctions userfunction = new userfunctions(); //jsonobject json = userfunction.rebuy(product_id, amount, quan); button btn_confirm = (button) findviewbyid(r.id.btn_confirm); button btn_home = (button) findviewbyid(r.id.btn_home); btn_confirm.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { databasehandler db = new databasehandler(getapplicationcontext()); string uid = db.getcurrentid(); system.out.println("shortalex" + uid); string product_id = lblpid.gettext().tostring().trim(); string amount = lblamount.gettext().tostring(); string quan = quantity.gettext().tostring().trim(); userfunctions userfunction = new userfunctions(); jsonobject json = userfunction.rebuy(product_id, amount, quan,uid); //databasehandler2 db = new databasehandler2(getapplicationcontext()); jsonobject json_user; try { system.out.println("shorty"); system.out.println(json.tostring()); json_user = json.getjsonobject("userbuy"); } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } toast.maketext(pagebuy.this, "purchase success", toast.length_short).show(); intent = new intent(getapplicationcontext(), pagepurchaserecord.class); startactivity(i); } }); btn_home.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { intent = new intent(getapplicationcontext(), main.class); startactivity(i); } }); } }
you need textwatcher this
quantity.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence s, int start, int before, int count) { // update quanttiti here } @override public void beforetextchanged(charsequence s, int start, int count, int after) { // todo auto-generated method stub } @override public void aftertextchanged(editable s) { // todo auto-generated method stub } });
Comments
Post a Comment