Getting multiple select checkbox value in android listview using Odoo mobile framework -



Getting multiple select checkbox value in android listview using Odoo mobile framework -

i using odoo mobile framework. android app connect odoo server. fetch product server , display in listview. want multiple select value listview. here code. when click checkbox, checkboxs checked automatically. not sure due oelistadapter or wrong code.

product_list.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/lvproductlistview" android:layout_width="match_parent" android:layout_height="match_parent" android:cliptopadding="false" android:paddingtop="?android:attr/actionbarsize" > </listview> </linearlayout>

product_list_row.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <imageview android:id="@+id/imgvproductpic" android:layout_width="42dp" android:layout_height="42dp" android:layout_gravity="center_vertical" android:src="@drawable/ic_launcher" /> <linearlayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" android:padding="5dp" > <textview android:id="@+id/txvproductname" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="product name" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/txvqty" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="quantity" android:textappearance="?android:attr/textappearancesmall" /> <textview android:id="@+id/txvprice" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:singleline="true" android:text="price" android:textappearance="?android:attr/textappearancesmall" /> </linearlayout> <checkbox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_marginleft="4dip" android:layout_marginright="10dip" android:focusable="false" android:focusableintouchmode="false" > </checkbox> </linearlayout>

here activity code.

public class products extends basefragment implements oetouchlistener.onpulllistener,adapterview.onitemclicklistener{ public static final string tag = "com.odoo.addons.products.products"; view mview = null; listview mlistview = null; oelistadapter mlistadapter = null; list<object> mproductitems = new arraylist<object>(); oetouchlistener mtouchattacher; productsloader mproductsloader = null; arraylist<integer> checkedpositions = new arraylist<integer>(); checkbox ckproduct; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { sethasoptionsmenu(true); mview = inflater.inflate(r.layout.products_list_layout, container, false); scope = new appscope(getactivity()); init(); homecoming mview; } /** * map view , product items */ private void init() { log.d(tag, "call init()"); mlistview = (listview) mview.findviewbyid(r.id.lvproductlistview); mlistadapter = new oelistadapter(getactivity(),r.layout.products_list_row, mproductitems) { @override public view getview(int position, view convertview, viewgroup parent) { view mview = convertview; final int holder = position; if (mview == null) mview = getactivity().getlayoutinflater().inflate(getresource(), parent, false); log.d(tag,"this "+position+"."); oedatarow row = (oedatarow) mproductitems.get(position); imageview imgproduct = (imageview) mview.findviewbyid(r.id.imgvproductpic); bitmap bitmap = null; if(row != null){ string base64image = row.getstring("image_small"); bitmap = base64helper.getbitmapimage(getactivity(), base64image); } imgproduct.setimagebitmap(bitmap); textview txvproductname, txvqty, txvprice; ckproduct = (checkbox) mview.findviewbyid(r.id.check); txvproductname = (textview) mview.findviewbyid(r.id.txvproductname); txvqty = (textview) mview.findviewbyid(r.id.txvqty); txvprice = (textview) mview.findviewbyid(r.id.txvprice); txvproductname.settext(row.getstring("name")); txvqty.settext(row.getstring("qty_available")); txvprice.settext(row.getstring("lst_price")); // when checkbox clicked, add/remove position to/from list ckproduct.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (((checkbox) v).ischecked()) { // if checked, add together list checkedpositions.add(holder); } else if(checkedpositions.contains(holder)) { // else if remove list (if present) checkedpositions.remove(holder); } } }); // set state of checbox based on if checked or not. ckproduct.setchecked(checkedpositions.contains(holder)); homecoming mview; } }; mtouchattacher = scope.main().gettouchattacher(); mtouchattacher.setpullableview(mlistview, this); // mlistview.setonitemclicklistener(this); mproductsloader = new productsloader(); mproductsloader.execute(); } // view.onitemclicklistener productcheckboxclicklistener = new view.onclicklistener() { // @override // public void onclick(view v) { // toast.maketext(getactivity(),"got",toast.length_short).show(); // } // }; // phone call productdb aka orm object @override public object databasehelper(context context) { homecoming new productsdb(context); } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { log.d(tag,"call oncreateoptionmenu"); inflater.inflate(r.menu.menu_fragment_products, menu); super.oncreateoptionsmenu(menu, inflater); } @override public boolean onoptionsitemselected(menuitem item) { log.d(tag,"call onoptionsitemselected"); switch (item.getitemid()) { case r.id.menu_product_next: toast.maketext(getactivity(),"test"+checkedpositions+"test",toast.length_short).show(); homecoming true; default: homecoming super.onoptionsitemselected(item); } } // add together menu drawer item @override public list<draweritem> drawermenus(context context) { list<draweritem> menu = new arraylist<draweritem>(); menu.add(new draweritem(tag, "products", true)); menu.add(new draweritem(tag, "products", 0, r.drawable.ic_action_todo,getfragment("products"))); homecoming menu; } // define fragment private fragment getfragment(string value) { products products = new products(); bundle bundle = new bundle(); bundle.putstring("name", value); products.setarguments(bundle); homecoming products; } /** * loading product list thread */ class productsloader extends asynctask<void, void, boolean> { @override protected boolean doinbackground(void ...params){ log.d(tag,"call productsloader()"); mproductitems.clear(); productsdb db = new productsdb(getactivity()); mproductitems.addall(db.select()); homecoming true; } @override protected void onpostexecute(boolean success) { log.d(tag,"call onpostexecute"); checkproducts(); } } /** * check empty database or not * todo need prepare check status */ private void checkproducts() { if(mproductitems.size() != 0){ mlistadapter.notifiydatachange(mproductitems); mlistview.setchoicemode(listview.choice_mode_multiple); mlistview.setadapter(mlistadapter); mlistview.setonitemclicklistener(this); } } /** * on product item click */ @override public void onitemclick(adapterview<?> adapter, view view, int position, long id) { oedatarow row = (oedatarow) mproductitems.get(position); toast.maketext(getactivity(),row.getstring("id"),toast.length_short).show(); } /** * watch sync status , inform productsloader */ @override public void onresume() { super.onresume(); getactivity().registerreceiver(msyncfinish, new intentfilter(syncfinishreceiver.sync_finish)); } @override public void onpause() { super.onpause(); getactivity().unregisterreceiver(msyncfinish); } syncfinishreceiver msyncfinish = new syncfinishreceiver() { public void onreceive(context context, android.content.intent intent) { mtouchattacher.setpullcomplete(); mproductsloader = new productsloader(); mproductsloader.execute(); } }; /** * on pulled sync message */ @override public void onpullstarted(view arg0) { scope.main().requestsync(productsprovider.authority); } }

i know late question , handling checkbox within listview should utilize viewholder pattern , create arraylist of boolean store selection here sample code

declaration of arraylist

public static arraylist<boolean> arrchecked;

initialise list

for (int = 0; < data.size(); i++) { arrchecked.add(false); }

then in getview method utilize holder ,

if (convertview == null) { convertview = inflater.inflate(r.layout.listview_item, null); viewholder viewholder = new viewholder(); viewholder.cb = (checkbox) convertview.findviewbyid(r.id.checkbox); // tag can object, happens viewholder convertview.settag(viewholder); } holder = (viewholder) convertview.gettag(); // set position id holder.cb.setid(position); holder.cb.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { int id = v.getid(); if (arrchecked.get(id)) { arrchecked.set(id, false); // unchecked state of checkbox } else { arrchecked.set(id, true); //do want in checked state here } } }); holder.cb.setchecked(arrchecked.get(position));

android listview checkbox odoo

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -