i've created 1 model class store product name,price , quantity,and attach fragment , try display in textview. unfortunately arraylist i'm not able position of items. following snippet code. need help.
modelclass arr_model.java
public class arr_model { private string brand_name; private string brnad_price; private string qty ; public arr_model(){} public string getbrand_name() { return brand_name; } public string getqty() { return qty; } public void setbrand_name(string brand_name) { this.brand_name = brand_name; } public void setbrnad_price(string brnad_price) { this.brnad_price = brnad_price; } public void setqty(string qty) { this.qty = qty; } public string getbrnad_price() { return brnad_price; } } here getting values response , store them in arraylist
productdetail.java
1)first getting data response,it product name , price
class loadalbums extends asynctask>> { /** * before starting background thread show progress dialog * */
@override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(all_product_details.this.getactivity()); pdialog.setmessage("loading..."); pdialog.setindeterminate(true); pdialog.setcancelable(false); pdialog.show(); } protected arraylist<hashmap<string,string>> doinbackground(string... args) { servicehandler sh = new servicehandler(); string jsonstr = sh.makeservicecall(user_url, servicehandler.get); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); jsonobject pname = jsonobj.getjsonobject(product_main); system.out.println("jsonobject product>>" + pname); jsonarray sizes = new jsonarray(); sizes = pname.getjsonarray("attributes"); (int j = 0; j < sizes.length(); j++) { jsonobject jsonnewtwo = sizes.getjsonobject(j); option = jsonnewtwo.getjsonarray("options"); system.out.println("option---" + option); } system.out.println("attribute size" + sizes); user_name = pname.getstring(user_name); prod_price = pname.getstring(product_price); prod_desc = pname.getstring(product_desc); } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } return null; } protected void onpostexecute(arraylist<hashmap<string,string>> result) { super.onpostexecute(result); if (pdialog.isshowing()) pdialog.dismiss(); if(user_name.equals("")) { uname.settext("not available"); } else { uname.settext(user_name); } if(prod_price.equals("")) { pprice.settext("not available"); } else { pprice.settext(dollars+prod_price); } if(prod_desc.equals("")) { general_desc.settext("not available"); } else { general_desc.settext(html.fromhtml(prod_desc).tostring()); } 2)then increase product quantity , click on add cart button,on click of add cart saving data in arraylist..
btn_add_to_cart.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { cartincrement(); productnams = genericutility.getstringfromsharedprefsforkey("selected_prodname", getactivity()); //system.out.println("seleced user name=-=-=-=-=-=-==-=" + productnams); productprc = genericutility.getstringfromsharedprefsforkey("selected_prodprc", getactivity()); //system.out.println("seleced prodprice=-=-=-=-=-=-==-=" + productprc); arr_model product = new arr_model(); product.setbrand_name(user_name); product.setbrnad_price(prod_price); product.setqty(_value.gettext().tostring()); productarraylist.add(product); system.out.println("test--" + productarraylist.get(0).getbrand_name()); } }); //system.out.println("test--" + productarraylist.get(0).getbrand_name()); mbtincrement.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { increment(); } }); _decrease.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { decrement(); } }); 3)then click on cart icon view item added quantity
relcart.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { _value.settext("1"); add_to_cart tf = new add_to_cart(); android.support.v4.app.fragmentmanager fm = getfragmentmanager(); android.support.v4.app.fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.frame_container, tf); ft.settransition(fragmenttransaction.transit_fragment_fade); ft.addtobackstack(null); ft.commit(); } }); } 4)now final step display items in listview,to next fragment
public class add_to_cart extends fragment { private button continue_shopping; private button checkout; listview list; private textview _decrease,mbtincrement,_value; private customlistadapter adapter; private arraylist<string> alst; private arraylist<string> alstprc; private string bname; private arraylist<arr_model> productarraylist; public add_to_cart(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { final view rootview = inflater.inflate(r.layout.list_view_addtocart, container, false); productarraylist = all_product_details.productarraylist; // alst=new arraylist<string>(); // bundle bundle = this.getarguments(); // alst = bundle.getstringarraylist("prodnam"); //consts.arr = new arraylist<string>(); //consts.arr = alst; // system.out.println("nam--" + productarraylist); // bundle bundle1 = this.getarguments(); // alstprc = bundle1.getstringarraylist("prodprc"); /* adapter = new customlistadapter(getactivity(),alst); list=(listview)rootview.findviewbyid(r.id.list_addtocart); list.setadapter(adapter); adapter.notifydatasetchanged(); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { } });*/ return rootview; } public class customlistadapter extends baseadapter { private context context; private arraylist<arr_model> listdata; //private aquery aquery; string dollars="\u0024"; public customlistadapter(context context,arraylist<arr_model> listdata) { this.context = context; this.listdata=listdata; // aquery = new aquery(this.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; } @override public view getview(final int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { holder = new viewholder(); convertview = layoutinflater.from(getactivity()).inflate(r.layout.list_item_addtocart, null); holder.txtproname = (textview) convertview.findviewbyid(r.id.proname_addtocart); holder.txtprofilecast = (textview) convertview.findviewbyid(r.id.proprice_addtocart); _decrease = (textview)convertview.findviewbyid(r.id.minuss_addtocart); mbtincrement = (textview)convertview.findviewbyid(r.id.plus_addtocart); _value = (edittext)convertview.findviewbyid(r.id.edt_procount_addtocart); convertview.settag(holder); }else{ holder = (viewholder) convertview.gettag(); } mbtincrement.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { increment(); } }); _decrease.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { decrement(); } }); /* for(int i=0;i<productarraylist.size();i++) { holder.txtproname.settext(productarraylist.get(0)); } holder.txtprofilecast.settext(dollars+alstprc.get(position));*/ // aquery.id(holder.propic).image(listdata.get(position).get(tag_image),true,true,0,r.drawable.ic_launcher); return convertview; } class viewholder{ imageview propic; textview txtproname; textview txtprofilecast; }
i can't follow code. if understand correctly, having problem passing data around between activity, asynctask , fragments. architecture here have activity keep track of data. fragment can ask activity data , asynctask can call activity update data.
if isn't enough information solve problem, please show place having trouble.
Comments
Post a Comment