Reputation: 594
I'm trying to do something simple and this was asked many times before in many variants but I couldn't manage to do it in my case..
I'm trying to do this scenario:
I have a popup alert dialog window (of some item) which has a delete button in it, and if you press the delete button it will popup another alert dialog of "are you sure you want to delete" with yes/no buttons.
If you press the yes it will delete the item and then I want both of the dialogs to be closed.
the yes/no dialog is closed by itself because I pressed the button, but the item dialog is not closed and I can't make it closed any how..
Here is my code:
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();
}
and here is the delete function:
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();
}
So as you can see, I'm passing the item dialog pointer into the yes/no dialog so it could close it, but the line just doesn't work..
Everything is running correctly on the debugger, when I reach to the line:
item_alert.dismiss();
it is pointed correctly to the item dialog, but this command just won't close it..
I tried the .close();
command also but same thing, nothing happens.
What am I missing? Thanks ahead!
Upvotes: 2
Views: 1055
Reputation: 594
Here is the correct answer thanks to 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();
}
}
Upvotes: 0
Reputation: 1485
You are calling
builder.show();
instead of
item_alert.show();
Upvotes: 1