I am having a problem in Android, when i set the adapter the list view is not updating.
This i use to delete one note from the database:
listaNotas.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, final long id) {
AlertDialog.Builder dialogo = new AlertDialog.Builder(Main.this);
dialogo.setTitle("Confirmação");
dialogo.setMessage("Deseja mesmo deletar a nota?");
dialogo.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
db.delete("Notas", "_id=?", (new String[]{String.valueOf(id)}));
Toast.makeText(Main.this, "Nota deletada com sucesso!", 5).show();
atualizaNota();
return;
}
});
dialogo.setNegativeButton("Não", null);
dialogo.show();
return false;
}
});
and this is the updates of the adapter:
public void atualizaNota() {
Cursor c = db.query("Notas", (new String[]{"_id", "Nota"}), "fgCompromisso=?", (new String[]{"0"}), null, null, "_id DESC");
if (c.getCount()==0)
return;
String[] from = {"Nota"};
int[] to = {R.id.edDescNota};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(Main.this, R.layout.layoutlistanota, c, from, to);
listaNotas.setAdapter(adapter);
}
Is there any problem within the code?
You should call
requery()to your cursor in order changes to be visible. Just declare the cursor as instance field and call it after deleting like:Also, don’t forget to close it
onPause()(if you did not done it already)