对话框消息按钮不执行代码内部的操作?

时间:2016-10-19 16:40:27

标签: android button dialog confirmation

我有一种从数据库中删除内容的方法。我想得到用户的确认。为了做到这一点,我实现了一个布尔函数来获得对话框的确认。

我的问题是,无论我选择是或否,我都会得到同样的错误结果。

(我使用了最后的boolean [] beacuse,否则我收到错误)

这是方法:

public boolean alertMessage() { //Confirmation to delete routine
    final boolean[] confirmation = {false};
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    // Yes button clicked
                    confirmation[0] =true;
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    // No button clicked
                    // do nothing
                    confirmation[0]=false;
                    break;
            }
        }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.sure_delete)
            .setPositiveButton(R.string.yes, dialogClickListener)
            .setNegativeButton(R.string.no, dialogClickListener).show();
    return confirmation[0];
}

这就是我在删除代码中检查的方式:

//CONFIRMATION
if(!alertMessage()){
 return;
}

更新 尝试使用一个答案建议但仍然相同:

public boolean alertMessage() { //Confirmation to delete routine
    final boolean[] confirmation = {false};
    new AlertDialog.Builder(this)
            .setTitle(R.string.delete)
            .setMessage(R.string.sure_delete)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    confirmation[0]=true;
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    confirmation[0]=false;
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
    return confirmation[0];
}

我把最后一个boolean [] becuase只用了一个布尔值我得到一个错误:

"Variable is accessed from within an inner class, needs to be declared final."

一旦我宣布最后:

"Can not assign a value to final variable"

所以我必须把它变成一个数组。 我希望它是一个布尔方法,并且不要在" yes"中实现删除,因为我想在其他方法中重用它。

3 个答案:

答案 0 :(得分:1)

这是因为当您显示对话框时,您的代码不会停止运行,因此您的方法将始终返回false,因为它是分配给它的默认值。要求删除确认的最佳方法是在对话框的正面按钮内调用删除方法。

假设您有一个列表视图,并且您希望在单击时删除该项目。这是你应该怎么做的。

public class MyActivity extends AppCompatActivity {


    public ListViewCompat listView;
    private List<Object> myObjects;

    public void onCreate(Bundle savedinstance) {
        super.onCreate(savedinstance);
        setContentView(R.layout.activity_my_list);

        listView = (ListViewCompat) findViewById(R.id.my_list_view);
        //Set a listview adapter

        listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                showConfirmationDialog(position);         
            }
        });
    }

    private void showConfirmationDialog(int itemPosition) {
        AlerDialog.Builder builder = new AlerDialog.Builder(this);

        builder.setTitle("Confirmation");
        builder.setMessage("Delete item?")
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            deleteItem(itemPosition);
        });
        builder.setNegativeButton("No", null);

        builder.create().show();
    }

    private void deleteItem(int itemPosition) {
        //Delete item
        myObjects.remove(itemPosition);
    }
}

答案 1 :(得分:0)

如您所愿,对同一列表中不同元素的不同操作使用相同的对话框:

public final static TAG_UPDATE = "update";
public final static TAG_DELETE = "delete";

public void alertMessage(String id, String actionTag) { //the same call for all elements of a list
final String mIdElement = idElement;
final String mActionTag = actionTag;

new AlertDialog.Builder(this)
        .setTitle(R.string.confirmation)
        .setMessage(R.string.sure_confirmation)
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                makeSomething(mIdElement, mActionTag);
            }
        })
        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //no-op
            }
        })
        .setIcon(android.R.drawable.ic_dialog_alert)
        .show();
}

public void makeSomething(String idElement, String actionTag){
    switch(actionTag) {
        case TAG_UPDATE :
            // your update code to update the id element
            break;
        case TAG_DELETE :
            // your delete code to delete the id element
            break;
    } 
}

答案 2 :(得分:0)

尝试使用接口:

public class MainActivity extends AppCompatActivity implements DialogInterface.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate");
        setContentView(R.layout.activity_main);

        new AlertDialog.Builder(this)
                .setTitle("Hello")
                .setMessage("World")
                .setPositiveButton(android.R.string.yes, this)
                .setNegativeButton(android.R.string.no, this)
        .show();

    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch(which) {
            case DialogInterface.BUTTON_POSITIVE:
                Log.d(TAG, "Ok");
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                Log.d(TAG, "Cancel");
                break;
        }

    }
}