从ArrayList(Android)删除之前的确认

时间:2016-04-06 12:53:00

标签: java android arraylist confirmation

我设置OnContextItemSelected以在用户尝试删除项目并要求他们确认时显示警告。问题是"确认已接收"变量在第一次执行代码时被设置为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;
}

1 个答案:

答案 0 :(得分:0)

做这样的事情

   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;
               if(confirmationReceived == true) {
                 AdapterView.AdapterContextMenuInfo menuInfo;
                 menuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
                int index = menuInfo.position;
                removeItem(index);
                confirmationReceived = false;
                return true;
                }
                dialog.dismiss();
            }

        })
        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        })
        .create();
return myRemovalDialogBox;
}