Android 列表视图的问题
我在 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);
}
代码中是否有任何问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该对光标调用
requery()
以使更改可见。只需将光标声明为实例字段并在删除后调用它,例如:另外,不要忘记关闭它
onPause()
(如果您还没有这样做)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)