我有一个带有删除和取消功能的自定义警报对话框。删除按钮是删除当前选中的列表项。取消按钮应关闭警报对话框并返回列表。当我单击删除时,它将删除它,当我单击取消时,它将关闭警报对话框并转到列表。但是,当我单击后退按钮时,它会再次显示警报对话框。这是我的警报对话框的代码片段:
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.grocerylistdelete, null);
AlertDialog.Builder abuilder = new AlertDialog.Builder(
context);
abuilder.setView(v);
TextView tvStatement = (TextView) v.findViewById(R.id.textViewStatementDelete);
tvStatement.setTypeface(bold);
tvStatement.setText("Are you sure you want to delete"+" "+mCursor.getString(mCursor.getColumnIndex(DBAdapter.KEY_ITEM))+"?");
Button delete = (Button) v.findViewById(R.id.buttonDelete);
delete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Cursor c = db.getAllGroceries();
c.moveToPosition(info.position);
String id = c.getString(c.getColumnIndex(DBAdapter.ROW_ID));
db.open();
db.deletegrocery(Long.parseLong(id));
c.close();
Toast toast = Toast.makeText(GroceryList.this, "Item Deleted Successfully", 5000);
toast.show();
Intent i = new Intent(getApplicationContext(), GroceryList.class);
fillData();
startActivity(i);
}
});
Button canceldelete = (Button) v.findViewById(R.id.buttonCancelDelete);
canceldelete.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), GroceryList.class);
startActivity(i);
}
});
abuilder.setCancelable(false);
AlertDialog alert2 = abuilder.create();
alert2.show();