java - Save checkbox state from a listview in an ArrayList -
i have been using listview
each item checkablelinearlayout
defined in
i cannot check row in listview
, not register check in ui. not believe there android:checkmark
checkablelinearlayout
. correct nest checkedtextview
inside checkablelinearlayout
, better if used checkbox
instead. also, how can put them in arraylist
checked, instead of iterating on them in manner.
this layout:
<com.example.deltest.checkablelinearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <checkedtextview android:id="@android:id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:checkmark="?android:attr/listchoiceindicatormultiple" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_a" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txt_b" /> </com.example.deltest.checkablelinearlayout>
i have implemented listactivity follows:
public class deletelist extends listactivity { arraylist<integer> idlist=new arraylist<integer>(); cursor c; handler handler; static final int what=0; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); final tumblrdb db=new tumblrdb(this); c=db.query(); startmanagingcursor(c); handler=new handler(); view v=getlayoutinflater().inflate(r.layout.layout_header, null,false); getlistview().addheaderview(v); getlistview().setchoicemode(listview.choice_mode_multiple); simplecursoradapter adapter=new simplecursoradapter(this,r.layout.layout_del,c,new string[] {tumblrdb.date,tumblrdb.desc},new int[]{r.id.txt_a,r.id.txt_b}); getlistview().setadapter(adapter); if(handler.hasmessages(what)) { c=db.query(); ((simplecursoradapter)getlistview().getadapter()).swapcursor(c); } button btn_del=(button)v.findviewbyid(r.id.btn_del); btn_del.setonclicklistener(new onclicklistener(){ @override public void onclick(view view) { // todo auto-generated method stub int count=getlistview().getcount(); sparsebooleanarray sparsebooleanarray=getlistview().getcheckeditempositions(); for(int i=0;i<count;i++) { if(sparsebooleanarray.get(i)) { c.movetoposition(i); int _id=c.getint(c.getcolumnindex(tumblrdb._id)); log.d("deletelist", "the id has been added "+_id); idlist.add(_id); new thread() { public void run() { for(int i:idlist) { db.delete(i); log.d("deletelist", "the id has been deleted "+i); handler.sendemptymessage(what); } } }.start(); } } } }); } }
edit trying different approach solve problem extending simplecursoradapter:
if(row==null) { row=inflater.inflate(layout, null, false); holder=new viewholder(row,position); row.settag(holder); holder.check_del.settag(position); } else { holder=(viewholder)row.gettag(); holder.check_del.setonclicklistener(null); holder.check_del.settag(position); if(hitlist.get(position)) holder.check_del.setchecked(true); else holder.check_del.setchecked(false); }
then have implemented onclicklistener checkbox:
onclicklistener cbl=new onclicklistener(){ @override public void onclick(view view) { // todo auto-generated method stub int position=(integer)view.gettag(); log.d("customcursoradapter", "the checkbox @ postion "+position+" has been clicked"); if(((checkbox)view).ischecked()) { hitlist.set(position, true); } else { hitlist.set(position, false); } } };
now when try access arraylist using,it throws arrayoutofboundsexception:
btn_del.setonclicklistener(new onclicklistener(){ @override public void onclick(view view) { // todo auto-generated method stub new thread() { public void run() { int count=lv.getcount(); for(int i=0;i<count;i++) { if(adapter.hitlist.get(i)) { db.delete(c.getint(c.getcolumnindex(tumblrdb._id))); } } } }.start(); adapter.notifydatasetchanged(); } });
using arraylist store state of checkboxes in list.
public static arraylist<boolean> hitlist;
i have initialized , set values false:
hitlist=new arraylist<boolean>(); for(int i=0;i<this.getcount();i++) { hitlist.add(i, false); }
when,initializing checkbox view,it looks in array position , checks if value in arraylist true.the position held checkbox using it's settag method , retrieved within listener determine position of checkbox has been checked.
@override public view getview(int position,view convertview,viewgroup parent) { view row=convertview; viewholder holder; if(row==null) { row=inflater.inflate(layout, null, false); holder=new viewholder(row); row.settag(holder); holder.check_del.settag(position); } else { holder=(viewholder)row.gettag(); holder.check_del.setonclicklistener(null); holder.check_del.settag(position); if(hitlist.get(position)) holder.check_del.setchecked(true); else holder.check_del.setchecked(false); } c.movetoposition(position); holder.txt_a.settext(c.getstring(c.getcolumnindex(itemdb.title))); holder.txt_b.settext(c.getstring(c.getcolumnindex(itemdb.description))); onclicklistener cbl=new onclicklistener(){ @override public void onclick(view view) { // todo auto-generated method stub int position=(integer)view.gettag(); log.d("customcursoradapter", "the checkbox @ postion "+position+" has been clicked"); if(((checkbox)view).ischecked()) { hitlist.set(position, true); } else { hitlist.set(position, false); } } }; holder.check_del.setonclicklistener(cbl); return row; } static class viewholder { textview txt_a; textview txt_b; checkbox check_del; viewholder(view row) { txt_a=(textview)row.findviewbyid(r.id.txt_a); txt_b=(textview)row.findviewbyid(r.id.txt_b); check_del=(checkbox)row.findviewbyid(r.id.check_del); } }
currently,i have header in listview has following onclicklistener:
@suppresswarnings("deprecation") @override public void onclick(view view) { // todo auto-generated method stub arraylist<boolean> killlist=customcursoradapter.hitlist; int totaldeleted=0; for(int i=0;i<killlist.size();i++) { //log.d(tag,i+":"+killlist.get(i)); c.movetoposition(i); string title=c.getstring(c.getcolumnindex(itemdb.title)); int _id=c.getint(c.getcolumnindex(itemdb._id)); //log.d(tag, "item has title "+title+" , id "+_id ); int rowsaffected=0; if(killlist.get(i)) { rowsaffected=db.delete(_id); //log.d(tag, "deleted row id "+_id); killlist.set(i, false); } totaldeleted+=rowsaffected; } //log.d(tag, "the total number of rows deleted "+totaldeleted); //if(c.requery()) //{ //log.d(tag, "requerying data,this calls notifydatasetchanged"); //} }});
once,you have deleted data row,before call requery on cursor(which deprecated...need find better alternative),the values in arraylist have reset.
Comments
Post a Comment