且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从列表视图,并删除数据库中的一个项目

更新时间:2023-02-04 10:06:18

当您设置这个的onClick 监听器按钮,还必须实现您的活动中的的onClick 方法。运行你的对话框,并执行删除操作的code将永远不会被调用你的情况。

When you set this as the onClick listener for a button, you must also implement the onClick method within your activity. The code that runs your dialog and performs the delete action will never be called in your case.

重新构建code到是这样的:

Refactor the code to something like:

    delete = (Button) findViewById(R.id.delete);
    delete.setOnClickListener(this);
}

public void onClick(View v)
{
    if(v.equals(delete))
    { 
        new AlertDialog.Builder(ShareFolioEditActivity.this) 
            .setTitle("Delete")
            .setMessage("Are you sure ??? ")
            .setNeutralButton("no",null)
            .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    delete();                
            }}).show();
    }
}

public void delete()
{  
    db.delete("sharelist", "_id="+ID, null);
    Toast.makeText(this, "row deleted"+ID, Toast.LENGTH_SHORT).show();
    Bundle b=null;
    onCreate(b);
}

请注意:我还没有选中此code,它可能无法编译,但它给你一个想法

Note: I've not checked this code, it might not compile, but it gives you an idea.