Multiple OK/Cancel dialogs, how to tell, in onClick(), which dialog?
android, android-dialog
Solution
Try something like this...
public class MyActivity extends Activity
implements DialogInterface.OnClickListener {
// Declare dialogs as Activity members
AlertDialog dialogX = null;
AlertDialog dialogY = null;
...
@Override
public void onClick(DialogInterface dialog, int which) {
if (dialogX != null) {
if (dialogX.equals(dialog)) {
// Process result for dialogX
}
}
if (dialogY != null) {
if (dialogY.equals(dialog)) {
// Process result for dialogY
}
}
}
}
EDIT This is how I create my `AlertDialogs`...
private void createGuideViewChooserDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Guide View")
.setSingleChoiceItems(guideViewChooserDialogItems, currentGuideView, this)
.setPositiveButton("OK", this)
.setNegativeButton("Cancel", this);
guideViewChooserDialog = builder.create();
guideViewChooserDialog.show();
}
Problem
I created a generic OkCancelDialog class that is conveniently invoked throughout my app via a static method: ``` static public void Prompt(String title, String message) { OkCancelDialog okcancelDialog = new OkCancelDialog(); okcancelDialog.showAlert(title, message); } ``` For various reasons I need the onClick listener in the activity, so in the activity I have: ``` public void onClick(DialogInterface v, int buttonId) { if (buttonId == DialogInterface.BUTTON_POSITIVE) { // OK button // do the OK thing } else if (buttonId == DialogInterface.BUTTON_NEGATIVE) { // CANCEL button // do the Cancel thing } else { // should never happen } } ``` This works great with a single dialog in the app, but now I want to add another OK/Cancel dialog, handled by the same activity. As far as I can tell, only one `onClick()` can be defined for the activity, so I am not sure how to go about implementing this. Any suggestions or tips?