我在Android中遇到问题,当我设置适配器时,列表视图没有更新。 这用于从数据库中删除一个注释:
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;
}
});
这是适配器的更新:
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);
}
代码中是否有任何问题?
答案 0 :(得分:0)
您应该将requery()
调用到光标,以便可以看到更改。只需将光标声明为实例字段,并在删除之后调用它:
public void onClick(DialogInterface arg0, int arg1) {
db.delete("Notas", "_id=?", (new String[]{String.valueOf(id)}));
c.requery(); // This is all you need
Toast.makeText(Main.this, "Nota deletada com sucesso!", 5).show();
atualizaNota();
return;
}
另外,不要忘记关闭它onPause()
(如果你还没有这样做)