我正在尝试做一些简单的事情,以前在许多变种中曾经多次被问过,但在我的情况下,我无法做到这一点。
我正在尝试这种情况:
我有一个弹出警告对话框窗口(某个项目),其中有一个删除按钮,如果你按下删除按钮,它会弹出另一个警告对话框“你确定要删除”,是/否的按钮。
如果按是,则会删除该项目,然后我希望关闭这两个对话框。
yes / no对话框本身是关闭的,因为我按下了按钮,但是项目对话框没有关闭,我也无法关闭它...
这是我的代码:
private void popupItem(final int item_id) {
//setup popup window
final AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(getActivity(), android.R.style.Holo_Light_ButtonBar_AlertDialog));
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialoglayout = inflater.inflate(R.layout.template_popup_item_alert, null);
builder.setView(dialoglayout);
builder.setTitle(getString(R.string.edit_item));
final AlertDialog item_alert = builder.create();
//setup delete button
final ImageButton deleteImageButton = (ImageButton) dialoglayout.findViewById(R.id.template_popup_item_delete);
deleteImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickDeleteItem(item_id, item_alert);
}
});
builder.show();
}
这是删除功能:
private void clickDeleteItem(final int item_id, final AlertDialog item_alert) {
// create (another) popup delete for "are you sure? yes/no"
AlertDialog.Builder alert_yesno = new AlertDialog.Builder(getActivity());
alert_yesno.setTitle(getString(R.string.want_to_remove));
alert_yesno.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// ... deleting the item(id)..
dialog.dismiss(); // close yes/on dialog (redundant - it is closed by itself anyway)
item_alert.dismiss(); // close the previous popup dialog //~~ NOT WORKING!
}
});
alert_yesno.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert_yesno.show();
}
正如你所看到的,我将项目对话框指针传递给yes / no对话框,以便它可以关闭它,但是这行不起作用..
当我到达该行时,调试器上的所有内容都正常运行:
item_alert.dismiss();
它正确指向项目对话框,但此命令不会关闭它..
我也尝试了.close();
命令,但同样的事情,没有任何反应。
我错过了什么? 谢谢你!
答案 0 :(得分:1)
您正在致电
builder.show();
而不是
item_alert.show();
答案 1 :(得分:0)
感谢Nishant Paradamwar,这是正确答案:
public class Popup{
AlertDialog item_alert; // *creating class object of it*
private void popupItem(final int item_id) {
//setup popup window
final AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(getActivity(), android.R.style.Holo_Light_ButtonBar_AlertDialog));
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialoglayout = inflater.inflate(R.layout.template_popup_item_alert, null);
builder.setView(dialoglayout);
builder.setTitle(getString(R.string.edit_item));
//setup delete button
final ImageButton deleteImageButton = (ImageButton) dialoglayout.findViewById(R.id.template_popup_item_delete);
deleteImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickDeleteItem(item_id); // *removed 2nd argument*
}
});
item_alert = builder.create(); // *moved the create to bottom so it will contain all parts*
item_alert.show(); // *calling the alert not builder*
}
private void clickDeleteItem(final int item_id) { // *removed second argument, its in class now*
// create (another) popup delete for "are you sure? yes/no"
AlertDialog.Builder alert_yesno = new AlertDialog.Builder(getActivity());
alert_yesno.setTitle(getString(R.string.want_to_remove));
alert_yesno.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// ... deleting the item(id)..
item_alert.dismiss(); // close the previous popup dialog //~~ *WORKING NOW!
}
});
alert_yesno.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert_yesno.show();
}
}