Activity:
public void delete(View v){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete this account?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
deleteCheque();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void deleteCheque(){
try {
db=new databaseone(this);
Details dt=new Details();
int rows=db.delete1(dt);
if ( rows == 1) {
Toast.makeText(this, "Cheque Deleted Successfully!", Toast.LENGTH_LONG).show();
this.finish();
}
else
Toast.makeText(this, "Could not delete cheque!", Toast.LENGTH_LONG).show();
}catch (Exception ex) {
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
Database:
public int delete1(Details dt) {
sdb=this.getWritableDatabase();
int rows = sdb.delete(TABLE_NAME, "id=?", new String[] { dt.getId()})
sdb.close();
return rows;
}
Logcat :
03-11 14:51:50.508: D/dalvikvm(384): GC_EXPLICIT freed 5844 objects / 303200 bytes in 363ms
03-11 14:52:05.698: D/dalvikvm(412): GC_EXTERNAL_ALLOC freed 951 objects / 64736 bytes in 78ms
03-11 14:52:20.648: V/one(412): 4
我是android的新手..我想使用警告对话框从数据库中删除所选数据..没有正常工作......任何人都可以帮助................ .................................................. .................... ................................
答案 0 :(得分:0)
我不知道细节 而且这个功能不太好。尝试改变这一点。
public Cursor delete1(Details dt) {
sdb=this.getWritableDatabase();
int rows = sdb.delete(TABLE_NAME, "id=?", new String[] { dt.getId() }); //Where is id?
return rows;
}
答案 1 :(得分:0)
你应该使用详细信息类的id,它在详细信息表中用作主键。
我认为id在Detail类中。检查详细类中的id。获得ID后,您可以使用
public int delete1(Details dt) {
sdb=this.getWritableDatabase();
int rows = sdb.delete(TABLE_NAME, "id=?", new String[] { dt.<id of detail class>});
sdb.close();
return rows;
}