【问题标题】:Confirmation before deleting from an ArrayList (Android)从 ArrayList 中删除之前的确认 (Android)
【发布时间】: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


【解决方案1】:

做这样的事

   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;
}

【讨论】:

  • 然后将项目传递给函数.. 像 AskRemoveConfirm(MenuItem item)
  • 然后通过AlertDialog diaBox = AskRemoveConfirm(item);
猜你喜欢
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
相关资源
最近更新 更多