警报确认对话框Android

时间:2016-07-16 13:49:15

标签: java android sdk

我一直在努力解决这个问题,但我无法

我想当用户点击btn_delete时,他会收到一条消息以确保删除(是或否),我已经尝试了很多方法,但我并不确切知道什么是'这个问题,我是Android编程的新手,请原谅我的愚蠢问题,这是我的Java代码:

public void onDeleteClick(View v) {
    int i = Integer.parseInt((String)v.getTag());
    Address address = _list.get(_currentPage*PANELS_PER_PAGE + i);
    _dbAdapter.deleteAddress(address.Id);

    _GetAddresses();
}

1 个答案:

答案 0 :(得分:14)

使用以下方法替换onDeleteClick方法:

public void onDeleteClick(View v) {
    int i = Integer.parseInt((String)v.getTag());

    AlertDialog.Builder alert = new AlertDialog.Builder(AddressListActivity.this);
    alert.setTitle("Delete");
    alert.setMessage("Are you sure you want to delete?");
    alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Address address = _list.get(_currentPage*PANELS_PER_PAGE + i);
            _dbAdapter.deleteAddress(address.Id);

            _GetAddresses();

            dialog.dismiss();
        }
    });

    alert.setNegativeButton("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
        }
    });

    alert.show();
}