android - Delete from database via ViewList MultiSelection -


i have next code.

public class listanotaciones extends listactivity {  anotacionesopenhelper openhelper = anotacionesopenhelper.getanotaciones(this); sqlitedatabase bd; cursor cursor;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     bd = openhelper.getreadabledatabase();     cursor = bd.rawquery("select * anotaciones", null);     final arraylist<cursor> lista = new arraylist<cursor>();      try {         string [] = {"titulo", "fecha"};         int [] = {r.id.anotacionestexto, r.id.anotacionesfecha};         final simplecursoradapter adapter = new simplecursoradapter(this, r.layout.anotacion, cursor, from, to, cursoradapter.flag_register_content_observer);         setlistadapter(adapter);          getlistview().setchoicemode(listview.choice_mode_multiple_modal);         getlistview().setmultichoicemodelistener(                 new abslistview.multichoicemodelistener() {                     @override                     public void onitemcheckedstatechanged(actionmode mode, int position, long id, boolean checked) {                         int seleccion = getlistview().getcheckeditemcount();                         boolean borrado = false;                          if(checked){                             lista.add((cursor)getlistview().getitematposition(position));                             lista.get(0).movetofirst();                             toast.maketext(getapplicationcontext(),lista.get(0).getstring(0),toast.length_short).show();                         }else{                             /*                             sqlitecursor seleccionado = (sqlitecursor)getlistview().getitematposition(position);                             for(int = 0;i < lista.size();i++){                                 if(lista.get(i).getstring(0).equals(seleccionado.getstring(0))){                                     lista.remove(i);                                     borrado = true;                                     break;                                 }                             }                             */                         }                         mode.settitle(seleccion + " notas seleccionadas");                     }                      @override                     public boolean oncreateactionmode(actionmode mode, menu menu) {                         mode.getmenuinflater().inflate(r.menu.ctx_borrar,menu); //inflamos el menĂº creado como xml en menu                         return true;                     }                      @override                     public boolean onprepareactionmode(actionmode mode, menu menu) {                         return false;                     }                      @override                     public boolean onactionitemclicked(actionmode mode, menuitem item) {                         switch (item.getitemid()){                             case r.id.ctxlblborrar:                                 /*                                 for(int = 0 ; < lista.size();i++){                                     toast.maketext(getapplicationcontext(),lista.get(i).getstring(0),toast.length_short).show();                                     bd.delete("anotaciones","_id = " + lista.get(i).getstring(0),null);                                 }                                 */                                 while(lista.get(0).movetonext()){                                     bd.delete("anotaciones","_id = " + lista.get(0).getstring(0),null);                                 }                                 adapter.notifydatasetchanged();                                 return true;                             default:                                 return false;                         }                      }                      @override                     public void ondestroyactionmode(actionmode mode) {                      }                 }         );     } catch (illegalargumentexception e) {         e.printstacktrace();     } }  /*@override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.anotaciones, menu);      return true; }*/  @override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()) {         case r.id.ctxlblborrar:             toast.maketext(getapplicationcontext(),"se ha pulsado borrar - " + getlistview().getcheckeditemcount(),toast.length_short).show();             return true;         default:             toast.maketext(getapplicationcontext(),"opcion no programada",toast.length_short).show();             return super.onoptionsitemselected(item);     } }  @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo){     super.oncreatecontextmenu(menu, v, menuinfo);     menuinflater inflater = getmenuinflater();     inflater.inflate(r.menu.ctx_borrar, menu); } } 

i'm trying delete database , update listview. i've seen several solutions here can't adapt solutions code (i'm pretty new android). more specifically, don't know how "save" options need delete. if need more code please so.

solved i'm sorry, i'm little blind, it's code question

public class listanotaciones extends listactivity {  anotacionesopenhelper openhelper = anotacionesopenhelper.getanotaciones(this); sqlitedatabase bd; cursor cursor; context contexto = this; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     bd = openhelper.getwritabledatabase();     cursor = bd.rawquery("select * anotaciones", null);     final arraylist<long> borrar = new arraylist<long>();     try {         string [] = {"titulo", "fecha", "texto"};         int [] = {r.id.anotacionestitulo, r.id.anotacionesfecha,r.id.anotacionestexto};         simplecursoradapter adapter = new simplecursoradapter(this, r.layout.anotacion, cursor, from, to, cursoradapter.flag_register_content_observer);         setlistadapter(adapter);         getlistview().setchoicemode(listview.choice_mode_multiple_modal);         getlistview().setmultichoicemodelistener(                 new abslistview.multichoicemodelistener(){                     @override                     public void onitemcheckedstatechanged(actionmode mode, int position, long id, boolean checked) {                         toast.maketext(getapplicationcontext(),long.tostring(id),toast.length_short).show();                         if(checked){                             borrar.add(id);                         }else{                             for(int = 0; < borrar.size();i++){                                 if(borrar.get(i) == id){                                     borrar.remove(i);                                 }                             }                             toast.maketext(getapplicationcontext(),"deseleccionado",toast.length_short).show();                         }                     }                      @override                     public boolean oncreateactionmode(actionmode mode, menu menu) {                         mode.getmenuinflater().inflate(r.menu.ctx_borrar,menu); //inflamos el menĂº creado como xml en menu                         return true;                     }                      @override                     public void ondestroyactionmode(actionmode mode) {                      }                      @override                     public boolean onactionitemclicked(actionmode mode, menuitem item) {                         switch (item.getitemid()){                             case r.id.ctxlblborrar:                                toast.maketext(getapplicationcontext(),"se ha pulsado borrar",toast.length_short).show();                                 for(int = 0 ; < borrar.size();i++){                                     bd.delete("anotaciones","_id=" + borrar.get(i),null);                                 }                                return true;                             default:                                 return false;                         }                     }                      @override                     public boolean onprepareactionmode(actionmode mode, menu menu) {                         return true;                     }                 }         );      } catch (illegalargumentexception e) {         e.printstacktrace();     } }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.anotaciones, menu);     return true; } } 

thanks anyway solves


Comments