views:

277

answers:

1

I'm creating a dialog as follows:

 @Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case DIALOG_1:
   return new AlertDialog.Builder(this)
   .setTitle(R.string.s_dlg1)
   .setPositiveButton(android.R.string.ok, this)
   .create();

  case DIALOG_2:
   ...
   ...
  }

  return null;
 }

 @Override
 public void onClick(DialogInterface dialog, int whichButton) {
  if (dialog == ???) {
   ...
  }
  else if (dialog == ???){
   ...
  }
 }

How do I identify which dialog triggered the onClick method? I can't declare the interface methods as in-line when creating the dialog because I want to access variables in my class. Every other interface passes some sort of id to its methods to identify which object called the method, but I can't seem to do anything with 'DialogInterface dialog'.

A: 

Perhaps you can extract the onclick listener as a seperate class and then pass in the dialog id? The interface is android.content.DialogInterface.OnClickListener

Hans
That would amount to the same as storing it in a private variable in the current class, since only one dialog can be shown at once, and the onClick will fire before the user can do anything to start another dialog and change the value. That's what I'm doing now, but it's unclean.I'm having the same problem with multiple DatePickerDialog on the same activity. There doesn't seem to be a straightforward way to identify which one fired the onDateSet event.
Kurian