【发布时间】:2016-07-26 21:27:00
【问题描述】:
我已设置 OnContextItemSelected 以在用户尝试删除项目并要求他们确认时显示警报。问题是“confirmationReceived”变量在第一次执行代码时设置为true,没有发生任何其他事情,然后在第二次执行时,在用户有机会确认他们想要删除之前删除项目.
onCreateContextMenu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Timetable Item");
menu.add(0, Remove_Item, Menu.NONE, R.string.Remove_Item);
}
onContextItemSelected
@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
AlertDialog diaBox = AskRemoveConfirm();
diaBox.show();
switch (item.getItemId()) {
case (Remove_Item): {
if(confirmationReceived == true) {
AdapterView.AdapterContextMenuInfo menuInfo;
menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = menuInfo.position;
removeItem(index);
confirmationReceived = false;
return true;
}
}
}
return false;
}
AskRemoveConfirm()
private AlertDialog AskRemoveConfirm()
{
AlertDialog myRemovalDialogBox =new AlertDialog.Builder(this)
.setTitle("Confirmation")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
confirmationReceived = true;
dialog.dismiss();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return myRemovalDialogBox;
}
【问题讨论】:
-
在确定按钮单击中执行删除任务...就在 dialog.dismiss() 之前
标签: java android arraylist confirmation