Android : checkbox in list view changing changing its state as we go from one activity to another and return back -
i beginner , creating list view checkboxes. when check checkboxes , return, come same activity checkbox unchecked. means lose state. tried save state in database. getting exception classcastexception. how achieve ? please help, code is,
public view getview(final int position, view convertview, viewgroup parent) { // event display final eventzz = (even) this.getitem(position); // child views in each row. final checkbox checkbox; textview textview; // create new row view if (convertview == null) { convertview = inflater.inflate(r.layout.eventsinfo, null); // find child views. textview = (textview) convertview .findviewbyid(r.id.rowtextview); checkbox = (checkbox) convertview.findviewbyid(r.id.checkbox01); checkbox.setbackgroundcolor(color.transparent); // optimization: tag row it's child views, don't // have call findviewbyid() later when reuse row. convertview.settag(new eventviewholder(textview, checkbox)); checkbox.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { int realposition = (integer) buttonview.gettag(); if (ischecked) { itemchecked.set(realposition, true); // update database store new checked item: contentvalues cv = new contentvalues(); cv.put("list_position", realposition); statusdb.insert("status", null, cv); } else { itemchecked.set(realposition, false); // delete position database because // unchecked statusdb.delete("status", "list_position = " + realposition, null); } } }); checkbox.setchecked(itemchecked.get(position)); // if checkbox toggled, update event tagged with. checkbox.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { int realposition = (integer) checkbox.gettag(); checkbox cb = (checkbox) v; eventz = (even) cb.gettag(); eventz.setchecked(cb.ischecked()); else { // because use viewholder, avoid having call // findviewbyid(). eventviewholder viewholder = (eventviewholder) convertview .gettag(); checkbox = viewholder.getcheckbox(); textview = viewholder.gettextview(); } checkbox.settag(new integer(position)); checkbox.setchecked(itemchecked.get(position)); // tag checkbox event displaying, can // access event in onclick() when checkbox toggled. checkbox.settag(eventzz); // display events data checkbox.setchecked(eventzz.ischecked()); textview.settext(eventzz.getname()); return convertview; }
helper.java
class helper extends sqliteopenhelper { public helper(context context, string name, cursorfactory factory, int version) { super(context, name, factory, version); } @override public void oncreate(sqlitedatabase db) { // list_position hold position list // checked string sql = "create table status (list_position integer);"; db.execsql(sql); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // interface } }
mainactivity.java
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.eventstoremindlist); // find listview resource. mainlistview = (listview) findviewbyid(r.id.mainlistview); checkbox = (checkbox) findviewbyid(r.id.checkbox01); arraylist<even> eventlist = new arraylist<even>(); eventlist.addall(arrays.aslist(events)); // set our custom array adapter listview's adapter. listadapter = new eventarrayadapter(this, eventlist, null); // retrieve list of position checked(if any) // database statusdb = mhelper.getwritabledatabase(); cursor statuscursor = statusdb.rawquery("select * status", null); int[] savedstatus = null; if ((statuscursor != null) & (statuscursor.movetofirst())) { savedstatus = new int[statuscursor.getcount()]; int = 0; { savedstatus[i] = statuscursor.getint(0); i++; } while (statuscursor.movetonext()); } // if cursor null or empty pass null savedstatus // adapter constructor , let handle(setting checkboxes // unchecked) mainlistview.setadapter(listadapter);
exception is,
10-07 13:07:15.922: e/androidruntime(816): fatal exception: main 10-07 13:07:15.922: e/androidruntime(816): java.lang.classcastexception: com.kalnirnay.kalnirnay.eventstoremindactivity$even 10-07 13:07:15.922: e/androidruntime(816): @ com.kalnirnay.kalnirnay.eventstoremindactivity$eventarrayadapter$1.oncheckedchanged(eventstoremindactivity.java:264) 10-07 13:07:15.922: e/androidruntime(816): @ android.widget.compoundbutton.setchecked(compoundbutton.java:124) 10-07 13:07:15.922: e/androidruntime(816): @ android.widget.compoundbutton.toggle(compoundbutton.java:86) 10-07 13:07:15.922: e/androidruntime(816): @ android.widget.compoundbutton.performclick(compoundbutton.java:98) 10-07 13:07:15.922: e/androidruntime(816): @ android.view.view$performclick.run(view.java:9080) 10-07 13:07:15.922: e/androidruntime(816): @ android.os.handler.handlecallback(handler.java:587) 10-07 13:07:15.922: e/androidruntime(816): @ android.os.handler.dispatchmessage(handler.java:92) 10-07 13:07:15.922: e/androidruntime(816): @ android.os.looper.loop(looper.java:123) 10-07 13:07:15.922: e/androidruntime(816): @ android.app.activitythread.main(activitythread.java:3683) 10-07 13:07:15.922: e/androidruntime(816): @ java.lang.reflect.method.invokenative(native method) 10-07 13:07:15.922: e/androidruntime(816): @ java.lang.reflect.method.invoke(method.java:507) 10-07 13:07:15.922: e/androidruntime(816): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 10-07 13:07:15.922: e/androidruntime(816): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 10-07 13:07:15.922: e/androidruntime(816): @ dalvik.system.nativestart.main(native method)
instead of saving state in database can use :-
1. static arraylist store position of checked checkbox, , read there checked value in getview() method before returning view ,
2. u can use sharedpreferences save state.
3. u can viewholder concept !!
i think such trivial use database not suitable .
it save u lines of codes !!
you can refer :-
Comments
Post a Comment