Android SimpleCrsorAdapter=新的SimpleCrsorAdapter(this,R.layout.anotacion,cursor,from,to,CursorAdapter.FLAG\u REGISTER\u CONTENT\u OBSERVER); setListAdapter(适配器); getListView().setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE\u MODE); getListView().SetMultichoiceModelListener( 新的AbsListView.MultichoiceModelListener(){ @凌驾 public void onItemCheckedStateChanged(ActionMode模式、int位置、长id、布尔选中){ Toast.makeText(getApplicationContext(),Long.toString(id),Toast.LENGTH\u SHORT.show(); 如果(选中){ borrar.add(id); }否则{ 对于(int i=0;i
无论如何谢谢你的解答只是我不知道如何正确删除。我知道“从数据库中删除,然后刷新屏幕”的原理,但我可以删除,但我不知道如何保存多重选择,然后再删除。您可以在onItemCheckedStateChanged()上保存位置。是的,但是,那么,我可以知道从de DB中删除的确切键是什么。我需要在db.delete()中传递一个参数Android SimpleCrsorAdapter=新的SimpleCrsorAdapter(this,R.layout.anotacion,cursor,from,to,CursorAdapter.FLAG\u REGISTER\u CONTENT\u OBSERVER); setListAdapter(适配器); getListView().setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE\u MODE); getListView().SetMultichoiceModelListener( 新的AbsListView.MultichoiceModelListener(){ @凌驾 public void onItemCheckedStateChanged(ActionMode模式、int位置、长id、布尔选中){ Toast.makeText(getApplicationContext(),Long.toString(id),Toast.LENGTH\u SHORT.show(); 如果(选中){ borrar.add(id); }否则{ 对于(int i=0;i,android,mysql,listview,Android,Mysql,Listview,无论如何谢谢你的解答只是我不知道如何正确删除。我知道“从数据库中删除,然后刷新屏幕”的原理,但我可以删除,但我不知道如何保存多重选择,然后再删除。您可以在onItemCheckedStateChanged()上保存位置。是的,但是,那么,我可以知道从de DB中删除的确切键是什么。我需要在db.delete()中传递一个参数 public class ListAnotaciones extends ListActivity { AnotacionesOpenHelper openHelper
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 * from anotaciones", null);
final ArrayList<Cursor> lista = new ArrayList<Cursor>();
try {
String [] from = {"titulo", "fecha"};
int [] to = {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 i = 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 i = 0 ; i < 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 the menu; this adds items to the action bar if it is 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);
}
}
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 * from anotaciones", null);
final ArrayList<Long> borrar = new ArrayList<Long>();
try {
String [] from = {"titulo", "fecha", "texto"};
int [] to = {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 i = 0; i < 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 i = 0 ; i < 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 the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.anotaciones, menu);
return true;
}
}