android - Is it possible to add a TextView to an Adapter Class and store the result in an Activity Class -
i using custom adapter class , want add textview , display in simple activity
user_cart.java activity public class user_cart extends fragmentactivity { cartadapter adapter; listview listview; arraylist<hashmap<string, string>> arraylist; public static string dish_name; public static string dish_cost; public static string dish_quantity; button order_btn; textview result; double quantitystrtemp, coststrtemp, totalstr, subtotalstr; private coordinatorlayout coordinatorlayout; databasehandler db = new databasehandler(this); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.user_cart); result = (textview) findviewbyid(r.id.subtotalet); order_btn = (button) findviewbyid(r.id.orderbt); arraylist = new arraylist<hashmap<string, string>>(); list<order> orders = db.getallorders(); log.d("tot", "" + db.getorderscount()); //db.deleteall(); (order cn : orders) { string log = "id: " + cn.getid() + " ,name: " + cn.getname() + " ,cost: " + cn.getcost() + " quantoty: " + cn.getquantity(); // writing contacts log /* dish_name = cn.getname(); dish_cost = cn.getcost(); dish_quantity = cn.getquantity();*/ log.d("name: ", log); hashmap<string, string> map = new hashmap<string, string>(); map.put("name", cn.getname()); map.put("cost", cn.getcost()); map.put("quantity", cn.getquantity()); arraylist.add(map); // listview.getselecteditem(); } listview = (listview) findviewbyid(r.id.userlistview); // pass results listviewadapter.java adapter = new cartadapter(user_cart.this, arraylist); // set adapter listview listview.setadapter(adapter); toolbar toolbar = (toolbar) findviewbyid(r.id.menus_toolbar); toolbar.setnavigationicon(r.mipmap.backbutton); calculatesum(); toolbar.setnavigationonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onbackpressed(); } }); order_btn.setonclicklistener(new view.onclicklistener() { boolean showtext = false; @override public void onclick(view v) { toast.maketext(user_cart.this, "thank order", toast.length_long).show(); db.deleteall(); intent intent = new intent(user_cart.this, mainactivity.class); startactivity(intent); } }); } @override public void onbackpressed() { //fragmentmanager fm = user_cart.this.getsupportfragmentmanager(); // fm.popbackstack(); //getfragmentmanager().popbackstackimmediate(); super.onbackpressed(); } public void calculatesum() { double sum = 0; list<order> orders = db.getallorders(); (order cn : orders) { try { //quantitystr = new double(et_dish_quan.gettext().tostring()); quantitystrtemp = double.valueof(cn.getquantity()); } catch (numberformatexception e) { quantitystrtemp = 0.0; // default value } try { //coststr = new double(et_menucost.gettext().tostring()); coststrtemp =double.valueof(cn.getcost()); } catch (numberformatexception e) { coststrtemp = 0.0; // default value } double resultstr = quantitystrtemp * coststrtemp; sum = sum + (resultstr); } log.d("sum",""+sum); } **cartadapter.java** public view getview(final int position, view convertview, viewgroup parent) { // declare variables final textview et_menuname, et_menucost, et_dish_quan; button btn_increment, btn_decrement; final textview totalet, costet, quantityet; imageview img_dishtype; final string rupee; rupee = context.getstring(r.string.rs); inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view itemview = inflater.inflate(r.layout.usercustomlistview, parent, false); resultp = data.get(position); btn_increment = (button) itemview.findviewbyid(r.id.increment_btn); btn_decrement = (button) itemview.findviewbyid(r.id.decrement_btn); totalet = (textview) itemview.findviewbyid(r.id.ptotal); et_menuname = (textview) itemview.findviewbyid(r.id.dish_nameet); et_menucost = (textview) itemview.findviewbyid(r.id.dish_costet); et_dish_quan = (textview) itemview.findviewbyid(r.id.dish_quantityet); et_menuname.settext(resultp.get("name")); et_menucost.settext(resultp.get("cost")); et_dish_quan.settext(resultp.get("quantity")); try { //quantitystr = new double(et_dish_quan.gettext().tostring()); quantitystr = double.parsedouble(et_dish_quan.gettext().tostring()); } catch (numberformatexception e) { quantitystr = 1.0; // default value } try { //coststr = new double(et_menucost.gettext().tostring()); coststr = double.parsedouble(et_menucost.gettext().tostring()); } catch (numberformatexception e) { coststr = 1.0; // default value } resultstr = coststr * quantitystr; stringdouble = double.tostring(resultstr); totalet.settext(stringdouble); empname = (resultp.get("name")); btn_increment.setonclicklistener(new view.onclicklistener() { boolean showtext = false; @override public void onclick(view v) { resultp = data.get(position); counter += 1; showtext = true; if (counter == 1 || counter < 1) { et_dish_quan.settext("1"); } else { et_dish_quan.settext(string.valueof(counter)); } try { quantitystr = new double(et_dish_quan.gettext().tostring()); } catch (numberformatexception e) { quantitystr = 1.0; // default value } try { coststr = new double(et_menucost.gettext().tostring()); } catch (numberformatexception e) { coststr = 1.0; // default value } resultstr = coststr * quantitystr; final string stringdouble = double.tostring(resultstr); totalet.settext(stringdouble); //db.updateorder(table_orders, cv, "_id=" + id, null); tempcost = double.tostring(coststr); tempquan = double.tostring(counter); db.addorder(new order(empname, tempcost, tempquan)); } }); btn_decrement.setonclicklistener(new view.onclicklistener() { boolean showtext = false; @override public void onclick(view v) { resultp = data.get(position); counter--; showtext = true; if (counter == 1 || counter < 1) { et_dish_quan.settext("1"); } else { et_dish_quan.settext(string.valueof(counter)); } try { quantitystr = new double(et_dish_quan.gettext().tostring()); } catch (numberformatexception e) { quantitystr = 1.0; // default value } try { coststr = new double(et_menucost.gettext().tostring()); } catch (numberformatexception e) { coststr = 1.0; // default value } resultstr = coststr * quantitystr; final string stringdouble = double.tostring(resultstr); totalet.settext(stringdouble); tempcost = double.tostring(coststr); tempquan = double.tostring(counter); db.addorder(new order(empname,tempcost,tempquan)); log.d("insert: ", "inserting .."); //uc.calculatesum(); } }); return itemview; }
sum of litiview item(resultstr). want addtion of resutstr. getting total value of cost , quantity.
//your activity class this:- public class mainactivity extends appcompatactivity { arraylist<string> listvalues; listview listview; textview textview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview=(listview)findviewbyid(r.id.list); textview=(textview)findviewbyid(r.id.textview); listvalues=new arraylist<string>(); listvalues.add("1"); listvalues.add("2"); listvalues.add("3"); listview= (listview) findviewbyid(r.id.list); customadapter= new customadapter(mainactivity.this, r.layout.myview, listvalues); listview.setadapter(customadapter); } //create method below in activity class public void updatevalue(string amount) { textview.settext(amount); }
your adapter this:-
private arraylist<currency> values; private context context; public customadapter(context context, int textviewresourceid, arraylist<string> values) { super(context, textviewresourceid, values); this.values=values; this.context=context; } @override public view getview(int position, view convertview, viewgroup parent) { view row = inflater.inflate(r.layout.spinner, parent, false); tempvalues = null; tempvalues = values.get(position); textview label = (textview) row.findviewbyid(r.id.tvlabel); button button=(button)row.findviewbyid(r.id.convert); label.settext(values.get(position)); button.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { int calculatedvalue=integer.parseint(label.gettext().tostring())*2; if(context instanceof mainactivity) { //update calculated value in activity textview ((mainactivity)context).update(string.valueof(calculatedvalue)); } } }); return row; }
Comments
Post a Comment