我有下一个代码。
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);
}
}
我正在尝试从数据库中删除然后更新ListView。我在这里看到了几个解决方案,但我无法使解决方案适应我的代码(我对android很新)。 更具体地说,我不知道如何“保存”选项而不是我需要删除的选项。 如果您需要更多代码,请说明。
答案 0 :(得分:0)
解决 我很抱歉,我有点失明, 这是我的问题的代码
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;
}
}
无论如何,谢谢你的解决方案