android - Checkbox not working properly on listview -
i made listview db , added onitemclicklistener. when click on item take me next activity , show full detail of item. added checkbox on listview checks item. problem when click on checkbox wont work, when click on item, starts next activity , after it, when go listview , try again click on checkbox, item checkbox works , other 1 doesn't work. when select second item listview , go checkbox works. checkbox works on item @ least 1 time click. have idea of causing problem? here's java:
public class datalistactivity extends activity { listview listview; sqlitedatabase sqlitedatabase; fooddbhelper fooddbhelper; cursor cursor; listdataadapter listdataadapter; listdataadapter dataadapter = null; button button; dataprovider dataprovider; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.data_list_layout); checkbuttonclick(); listview = (listview)findviewbyid(r.id.list_view); listdataadapter = new listdataadapter(getapplicationcontext(),r.layout.row_layout); listview.setadapter(listdataadapter); fooddbhelper = new fooddbhelper(getapplicationcontext()); sqlitedatabase = fooddbhelper.getreadabledatabase(); cursor = fooddbhelper.getinformations(sqlitedatabase); if (cursor.movetofirst()) { { string name,quantity,calorie,fat,protein,sugar,vitamins; boolean selected = false; string names = null; name = cursor.getstring(0); quantity = cursor.getstring(1); calorie = cursor.getstring(2); fat = cursor.getstring(3); protein = cursor.getstring(4); sugar = cursor.getstring(5); vitamins = cursor.getstring(6); dataprovider dataprovider = new dataprovider(name,quantity,calorie,fat,protein,sugar,vitamins,names,selected); listdataadapter.add(dataprovider); }while (cursor.movetonext()); } listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent,view view,int position, long id) { string name = (string) ((textview) view.findviewbyid(r.id.text_dish_name)).gettext(); string quantity = (string) ((textview) view.findviewbyid(r.id.text_dish_quantity)).gettext(); string calorie = (string) ((textview) view.findviewbyid(r.id.text_dish_calorie)).gettext(); string fat = (string) ((textview) view.findviewbyid(r.id.text_dish_fat)).gettext(); string protein = (string) ((textview) view.findviewbyid(r.id.text_dish_protein)).gettext(); string sugar = (string) ((textview) view.findviewbyid(r.id.text_dish_sugar)).gettext(); string vitamins = (string) ((textview) view.findviewbyid(r.id.text_dish_vitamins)).gettext(); string.valueof(parent.getitematposition(position)); toast.maketext(getapplicationcontext(), "dish name : " + name, toast.length_short).show(); checkbox names = (checkbox) view.findviewbyid(r.id.checkbox1); listview.setonitemclicklistener(this); names.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkbox checkbox = (checkbox) v; dataprovider dataprovider = (dataprovider) checkbox.gettag(); toast.maketext(getapplicationcontext(), "clicked on checkbox: " + dataprovider.getname() + checkbox.gettext() + " " + checkbox.ischecked(), toast.length_short).show(); dataprovider.setselected(checkbox.ischecked()); } }); intent intent=new intent(getapplicationcontext(),detail.class); intent.putextra("dish name",name); intent.putextra("dish quantity",quantity); intent.putextra("dish calorie",calorie); intent.putextra("dish fat",fat); intent.putextra("dish protein",protein); intent.putextra("dish sugar",sugar); intent.putextra("dish vitamins",vitamins); startactivity(intent); } });} private void checkbuttonclick() { list list = new arraylist(); listdataadapter = new listdataadapter(this, r.layout.row_layout,list); button mybutton = (button) findviewbyid(r.id.findselected); mybutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { stringbuffer responsetext = new stringbuffer(); responsetext.append("the following dishes selected...\n"); arraylist<dataprovider> list = (arraylist<dataprovider>) listdataadapter.list; for(int i=0;i<list.size();i++) { dataprovider dataprovider = list.get(i); if (dataprovider.isselected()) { responsetext.append("\n" + dataprovider.getname()+" : " + dataprovider.getcalorie()+" kcal"); } } toast.maketext(getapplicationcontext(), responsetext, toast.length_long).show(); } }); } } public class dataprovider { private string name = null; private string quantity; private string calorie; private string fat; private string protein; private string sugar; private string vitamins; private string names = null; boolean selected = false; public string getname(){ return name; } public void setname(string name){ this.name = name; } public string getquantity(){ return quantity; } public void setquantity(string quantity){ this.quantity = quantity; } public string getcalorie(){ return calorie; } public void setcalorie(string calorie){ this.calorie = calorie; } public string getfat(){ return fat; } public void setfat(string fat){ this.fat = fat; } public string getprotein(){ return protein; } public void setprotein(string protein){ this.protein = protein; } public string getsugar() {return sugar; } public void setsugar(string sugar) {this.sugar = sugar ;} public string getvitamins() {return vitamins; } public void setvitamins(string vitamins) {this.vitamins = vitamins ;} public string getnames() {return names; } public void setnames(string names) {this.names = names ;} public boolean isselected() {return selected;} public void setselected(boolean selected) {this.selected = selected;} public dataprovider(string name,string quantity,string calorie, string fat,string protein,string sugar,string vitamins,string names, boolean selected){ this.name = name; this.quantity = quantity; this.calorie = calorie; this.fat = fat; this.protein = protein; this.sugar = sugar; this.vitamins = vitamins; this.names = names; this.selected = selected; } public void add(list list) { } } public class listdataadapter extends arrayadapter { list list = new arraylist(); public listdataadapter(context context, int resource) { super(context, resource); } public listdataadapter(datalistactivity datalistactivity, int row_layout, list list) { super(datalistactivity, row_layout, list); } static class layouthandler{ textview name,quantity,calorie,fat,protein,sugar,vitamins; checkbox names; } @override public void add(object object) { super.add(object); list.add(object); } @override public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; layouthandler layouthandler; if (row == null) { layoutinflater layoutinflater = (layoutinflater)this.getcontext().getsystemservice(context.layout_inflater_service); row = layoutinflater.inflate(r.layout.row_layout,parent,false); layouthandler = new layouthandler(); layouthandler.name = (textview)row.findviewbyid(r.id.text_dish_name); layouthandler.quantity = (textview)row.findviewbyid(r.id.text_dish_quantity); layouthandler.calorie = (textview)row.findviewbyid(r.id.text_dish_calorie); layouthandler.fat = (textview)row.findviewbyid(r.id.text_dish_fat); layouthandler.protein = (textview)row.findviewbyid(r.id.text_dish_protein); layouthandler.sugar = (textview)row.findviewbyid(r.id.text_dish_sugar); layouthandler.vitamins = (textview)row.findviewbyid(r.id.text_dish_vitamins); layouthandler.names = (checkbox) row.findviewbyid(r.id.checkbox1); row.settag(layouthandler); } else { layouthandler = (layouthandler) row.gettag(); } dataprovider dataprovider = (dataprovider)this.getitem(position); layouthandler.name.settext(dataprovider.getname()); layouthandler.quantity.settext(dataprovider.getquantity()); layouthandler.calorie.settext(dataprovider.getcalorie()); layouthandler.fat.settext(dataprovider.getfat()); layouthandler.protein.settext(dataprovider.getprotein()); layouthandler.sugar.settext(dataprovider.getsugar()); layouthandler.vitamins.settext(dataprovider.getvitamins()); layouthandler.names.setchecked(dataprovider.isselected()); layouthandler.names.settag(dataprovider); return row; } }
thanks in advance
you need of things:
step 1:
need modify getview..
here need add action checkbox
example :
layouthandler.names.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkbox checkbox = (checkbox) v; dataprovider dataprovider = (dataprovider) checkbox.gettag(); toast.maketext(getapplicationcontext(), "clicked on checkbox: " + dataprovider.getname() + checkbox.gettext() + " " + checkbox.ischecked(), toast.length_short).show(); dataprovider.setselected(checkbox.ischecked()); } });
step 2:
once add checkbox in listview row click action causing issues. can solve need trigger itemclick action getview method
example:
//i adding widget layouthandler.layout = (linearlayout)row.findviewbyid(r.id.linear_layout);
then click action
layouthandler.layout.setonclick.....
under trigger item click action using performitemclick
layouthandler.layout.performitemclick(...
Comments
Post a Comment