다음 코드가 있습니다.
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를 업데이트하려고합니다. 여기에서 몇 가지 솔루션을 보았지만 솔루션을 내 코드에 적용 할 수 없습니다 (나는 안드로이드를 처음 접했습니다). 보다 구체적으로, 삭제해야하는 것보다 옵션을 "저장"하는 방법을 모릅니다. 더 많은 코드가 필요하면 그렇게 말 해주세요.
SOLVED 정말 미안 해요, 저는 약간 눈이 멀었습니다. 제 질문에 대한 코드입니다.
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;
}
}
어쨌든 해결해 주셔서 감사합니다
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다