android - delete item in listview by capturing checkbox selection in listview -


i using custom list view has check box , button , populating list view using cursor adapter.

now trying capture checkbox selection in listview , delete listview item using checkbox selection.. after lot of googling found out sparseboolean array wil solve purpose. have used capture checked checkbox array returning null. below code

public class stockmanager extends activity{      string getentry;     private int storeid=0;     //database d;     stocktable st;     private string getstocks;     public cursor a1;     intent bd;     intent sd;     //listview popstocks;      /*public stockmanager() {         // todo auto-generated constructor stub      }*/      static class viewholder {          checkbox cb;         button view1;      }       protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.stockmanager);         // d=new database(getapplicationcontext());         st=new stocktable(getapplicationcontext());         final button addstock=(button) findviewbyid(r.id.button1);         final button deletestock=(button) findviewbyid(r.id.button3);         final edittext entry=(edittext) findviewbyid(r.id.edittext1);         final button brodetail=(button) findviewbyid(r.id.button2);         //final listview popstocks=(listview) findviewbyid(r.id.listview1);         final listview popstocks =(listview) findviewbyid(r.id.listview1);         final textview displaystocks=(textview) findviewbyid(r.id.textview2);          final viewholder myviewholder1=new viewholder();         myviewholder1.cb=(checkbox) findviewbyid(r.id.checkbox1);          displaystocks.settext("you have not added stocks start entering stock name , clicking add stock button");         getstocks="select " + st.column1 + " _id, " + st.column3 + " "+ st.tablename;         a1=database.getinstance(getapplicationcontext()).getwritabledatabase().rawquery(getstocks, null);         if(a1.movetonext())         {             displaystocks.setvisibility(view.invisible);         }         poplist populatestocks=new poplist(getapplicationcontext(),a1) ;         popstocks.setadapter(populatestocks);            addstock.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                   getentry=entry.gettext().tostring();                  system.out.println(getentry);                  database.getinstance(getapplicationcontext()).getwritabledatabase().insert(st.tablename, null,st.insert(getentry));                     system.out.println(getstocks);                   a1=database.getinstance(getapplicationcontext()).getwritabledatabase().rawquery(getstocks, null);                 if(a1.movetonext())                 {                     displaystocks.setvisibility(view.invisible);                 }                   poplist populatestocks=new poplist(getapplicationcontext(),a1) ;                 //populatelist populatestocks=new populatelist();                 popstocks.setadapter(populatestocks);               }         });          /*  myviewholder1.cb.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 system.out.println("item clicked");                 system.out.println(myviewholder1.cb.ischecked());              }         });*/          brodetail.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 bd=new intent(v.getcontext(),brokeragedetail.class);                 startactivity(bd);             }         });         ******in below button want capture listview checkbox selection***************************         deletestock.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                  // todo auto-generated method stub                  sparsebooleanarray checkeditems=popstocks.getcheckeditempositions();                  //system.out.println(checkeditems.valueat(checkeditems.size()));                 system.out.println(popstocks.getcheckeditempositions());                 //system.out.println(popstocks.isitemchecked(popstocks.getcheckeditemposition()));             }         });                 }       public class poplist extends cursoradapter{          public poplist(context context, cursor c) {             super(context, c);             // todo auto-generated constructor stub         }         //stocktable st1=new stocktable(getapplicationcontext());         //database d1=new database(getapplicationcontext());           @override         public void bindview(view view, context context, cursor c) {             // todo auto-generated method stub             final viewholder myviewholder=new viewholder();             myviewholder.cb=(checkbox) view.findviewbyid(r.id.checkbox1);             myviewholder.view1=(button) view.findviewbyid(r.id.button1);               //if(c.movetofirst()){                  //cb.settext(a1.getstring(a1.getcolumnindex(st1.column2)));                     //do{                         //cb.settext(a1.getstring(a1.getcolumnindexorthrow(st.column2)));             myviewholder.cb.settext(c.getstring(c.getcolumnindexorthrow(st.column3)));                 //  }while (a1.movetonext());              //  }              myviewholder.cb.setonclicklistener(new onclicklistener() {                  @override                 public void onclick(view v) {                     // todo auto-generated method stub                     system.out.println("item clicked");                     system.out.println(myviewholder.cb.ischecked());                  }             });              myviewholder.view1.setonclicklistener(new onclicklistener() {                  @override                 public void onclick(view v) {                     // todo auto-generated method stub                     sd=new intent(v.getcontext(),stockdetail.class);                     sd.putextra("stockname", myviewholder.cb.gettext());                     startactivity(sd);                   }             });         }          @override         public view newview(context context, cursor c, viewgroup parent) {             // todo auto-generated method stub             layoutinflater inflater = layoutinflater.from(context);              view v = inflater.inflate(r.layout.stocklist, parent, false);                     bindview(v, context, c);                    return v; //          return null;         }       }      public void declerations(){       }   } 

please me solve this.. please guide me if going in wrong direction.. trying last few days without success

first of have save checkbox values. after save try checked item in listview.

sparsebooleanarray checked = list.getcheckeditempositions();  (int = 0; < list.getadapter().getcount(); i++) {     if (checked.get(i)) {         //     } } 

or try link checked values delete or want.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -