如何在Android中删除SQL中的行? [英] How to delete rows in SQL in android?

查看:65
本文介绍了如何在Android中删除SQL中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个记事本应用程序,当您在列表活动中长按一个笔记时,它将创建一个对话框,询问您是否要删除该笔记,而当您单击是"时,它将删除该笔记.但是由于某种原因,这不起作用,并且不是因为listview无法刷新.

LongItemClick侦听器:

I have a notepad application that when you long click on a note in a list activity it will create a dialog that will ask you if you want to delete the note, and when you click yes it will delete the note. But for some reason this doesn''t work and it is not becuase the listview does not refresh.

LongItemClick Listener:

@Override
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Delete...");
    alertDialog.setMessage("Are you sure you want to delete this note?");
    alertDialog.setCancelable(false);

    final int p = position;

    alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            helper.delete(p);
        }
    });

    alertDialog.setNegativeButton("No", null);
    alertDialog.show();

    return true;
}



删除行方法:



Delete row method:

public void delete(int id) {
    getWritableDatabase().delete("notes", "_ID="+Integer.toString(id), null);
}

推荐答案

假设您使用的是CurstorAdapter,则需要使用id,而不是删除位置.位置只是列表中的顺序,它可能与id相等,但并不总是如此,并假设是这样,它可能会导致数据丢失.
Assuming you are using a CurstorAdapter, you will need to use the id, not the position for deleting. The position, is just the order in the list, it could potentially be equal to the id, but isn''t always and to assume so, it could cause data loss.


这篇关于如何在Android中删除SQL中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆