单击方法上的警报对话框不起作用

时间:2013-09-20 06:24:51

标签: android

我有一个简单的警告对话框的代码,如下所示。

enter image description here

这是我的代码:

private boolean toggle;

//an onCreate method here

public boolean onOptionsItemSelected(MenuItem item) {  
        switch (item.getItemId()) {       
        case R.id.deleteContact:
            if (selectedContactIndex == -1){
                Context context = getApplicationContext();
                CharSequence text = "Please select a contact to delete!";
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                return true;
            }
            AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
            myAlertDialog.setTitle("           Confirm Contact Deletion");
            myAlertDialog.setMessage("Are you sure you want to delete " + Contact.contactList.get(selectedContactIndex).getFullName() + "?");

            myAlertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                toggle = true;
            }});

            myAlertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                toggle = false;
            }});

            myAlertDialog.show();
            System.out.println(toggle);
            if (toggle) {
                Contact.contactList.remove(selectedContactIndex);
                for (int i = selectedContactIndex; i < Contact.contactList.size(); i++){
                    Contact.contactList.get(i).setContactNumber(i);
                }
                Contact.totalContacts--;
                selectedContactIndex = -1;
                contactsView.invalidateViews();
            }
    }

所以我做的方式是,我有一个名为toggle的布尔值。如果他们单击是,则布尔值设置为true,并执行if语句,实际删除联系人。我已经使用了system.out.print并检查了日志猫,显然即使单击“是”按钮后,切换变量也为false。

我的一个假设是:布尔值的默认值为false,因此onClick方法甚至可能无法为切换分配任何内容。但我不知道如何解决这个问题。

任何帮助都将不胜感激。

编辑:感谢您的回答,我将联系人删除逻辑放在了正面按钮的onClick方法中,它的工作方式就像我想要的那样!我从中得到的主要是要意识到android并不总是线性执行。

5 个答案:

答案 0 :(得分:3)

获取toggle always false的实际原因是所有函数同时执行,即在对dailog执行任何操作之前if statment is also execute

解决方案@您在if statementtoggle=true点击对话框切换设置为true时执行Yes,因此请将if statement放在yes click对话框上外。

AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
            myAlertDialog.setTitle("           Confirm Contact Deletion");
            myAlertDialog.setMessage("Are you sure you want to delete " + Contact.contactList.get(selectedContactIndex).getFullName() + "?");

            myAlertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                //toggle = true;
                 //Here will be your if statement.
            }});

            myAlertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
               // toggle = false;
            }});

            myAlertDialog.show();
            System.out.println(toggle);

            }
    }

答案 1 :(得分:0)

如果您的默认值为 false ,并且在删除联系人之后刷新您的活动,而不是将您的切换值设置为false,您在“是”按钮中单击<更改< / p>

答案 2 :(得分:0)

如果将逻辑放在onclick侦听器中而不是在其外部使用if语句,那可能会更好。尝试这样的事情。

AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
     myAlertDialog.setTitle("           Confirm Contact Deletion");
     myAlertDialog.setMessage("Are you sure you want to delete " + Contact.contactList.get(selectedContactIndex).getFullName() + "?");

     myAlertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    toggle = true;
                    Contact.contactList.remove(selectedContactIndex);
                for (int i = selectedContactIndex; i < Contact.contactList.size(); i++){
                    Contact.contactList.get(i).setContactNumber(i);
                }
                Contact.totalContacts--;
                selectedContactIndex = -1;
                contactsView.invalidateViews();
     }});

     myAlertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    toggle = false;
     }});


     myAlertDialog.show();

答案 3 :(得分:0)

嘿配合onclick监听器中的函数只有在用户操作后才能通过单击yes或no来调用,此时你的代码删除联系人已经执行,你的代码执行不是线性的,稍后会调用click listener函数。

尝试这样的事情让你朝着正确的方向前进

myAlertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
        Contact.contactList.remove(selectedContactIndex);
        for (int i = selectedContactIndex; i < Contact.contactList.size(); i++){
            Contact.contactList.get(i).setContactNumber(i);
        }
        Contact.totalContacts--;
        selectedContactIndex = -1;
        contactsView.invalidateViews();
    }
});

答案 4 :(得分:-2)

将变量声明为private boolean toggle = true;