Android 列表视图的问题

发布于 2024-11-18 11:28:32 字数 1529 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-11-25 11:28:32

您应该对光标调用 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() (如果您还没有这样做)

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:

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;
}

Also, don't forget to close it onPause() (if you did not done it already)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文