【发布时间】:2017-03-23 17:48:23
【问题描述】:
我有一个简单的问题,我不知道如何解决。 我有一个管理联系人的应用程序,它可以添加、更新、保存和删除 我想要的只是在单击删除按钮时出现一个带有“是”和“否”选项的确认对话框。
目前我得到一个错误,它不喜欢我尝试访问我的数据库以让它从数据库表中删除联系人。
这是我的代码:
} else if (view == findViewById(R.id.delete)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete this contact?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
UserSQL repo = new UserSQL(this);
repo.delete(_contact_Id);
Toast.makeText(this, "Contact Record Deleted", Toast.LENGTH_SHORT).show();
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
} else if (view == findViewById(R.id.close)){
finish();
}
它不喜欢的那一行是
UserSQL repo = new UserSQL(this);
repo.delete(_contact_Id);
Toast.makeText(this, "Contact Record Deleted", Toast.LENGTH_SHORT).show();
finish();
问题在于new UserSQL(this),它不喜欢我如何调用我的数据库。仅当我尝试将其放入对话框方法时才会出现此问题。
我的代码在没有确认的情况下完美运行:
else if (view == findViewById(R.id.delete)) {
UserSQL repo = new UserSQL(this);
repo.delete(_contact_Id);
Toast.makeText(this, "Contact Record Deleted", Toast.LENGTH_SHORT).show();
finish();
感谢您的帮助,我研究了这个问题,但不知道为什么,如果可能的话,我希望有一个很好的解释,也许还有另一个建议,以更简单的方式做到这一点。
【问题讨论】:
-
尝试创建另一个包含您的数据库代码的方法,并在您的肯定按钮中使用该方法。
-
发布您的错误日志
-
还是同样的错误,基本上是我上面使用的代码,如果你看onClick方法里面的代码,它不喜欢UserSQL后面的“(this)”
-
已经被下方@Shark解决了,谢谢各位大佬的帮助
标签: java android sqlite dialog