Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify which AlertDialog triggered onClick(DialogInterface dialog, int which)

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'.

like image 859
Monstieur Avatar asked Feb 22 '10 06:02

Monstieur


1 Answers

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

like image 59
Hans Avatar answered Sep 30 '22 04:09

Hans