tags:

views:

5192

answers:

5

I want to show an AlertDialog with one option that might change on every request. So for example at one time I want to show the option "add to contacts" while another time it should be "remove from contacts".

My code does work on the first time, however Android seems to cache the AlertDialog so that onCreateDialog is not executed next time. Therefore the option doesnt change anymore. Can I prevent this caching, or is there just another way of changing the option?

I am working with SDK 1.5 but using 1.1.

@Override
protected Dialog onCreateDialog(final int id) {
    ...
    String add_remove_contact = res.getString(R.string.profile_add_to_contacts);
 if (user.getContacts().contains(profileID)) {
     add_remove_contact = res.getString(R.string.profile_remove_from_contacts);
     // TODO: this string is not changed when contact status changes 
    }
    final CharSequence[] items = {res.getString(R.string.view_profile),
          res.getString(R.string.profile_send_message),
          add_remove_contact};
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 ...
 return builder.create();
}
+5  A: 

Take a look at onPrepareDialog method that will be called before dialog is shown. There You can change the required values based on request type.

Example with date picker

@Override
protected Dialog onCreateDialog(final int id) {
  switch (id) {
  case DIALOG_DATE_ID:
    final Calendar c = Calendar.getInstance();
    return new DatePickerDialog(this, this, c.get(Calendar.YEAR),
                                c.get(Calendar.MONTH), 
                                c.get(Calendar.DAY_OF_MONTH));
  default:
    return super.onCreateDialog(id);
  }
}

@Override
protected void onPrepareDialog(final int id, final Dialog dialog) {
  switch (id) {
  case DIALOG_DATE_ID:
    //update to current time
    final Calendar c = Calendar.getInstance();
    ((DatePickerDialog) dialog).updateDate(c.get(Calendar.YEAR), 
                                           c.get(Calendar.MONTH), 
                                           c.get(Calendar.DAY_OF_MONTH));
    break;
  }
}
JaanusSiim
onPrepareDialog is a good place to update the dialog. But how can I change an AlertDialogs items in onPrepareDialog? There doesn't seem to be any method for this and I cant find an id to use with findViewById.
Ulrich Scheller
A: 

You can also use the removeDialog(int) function of the Activity. When a dialog is dismissed, the Activity basically stores the state of the dialog (for performance reasons I would imagine). Calling removeDialog(int) on the dialog forces the activity to unload all references for the dialog and dismisses it from the screen if it's being shown.

Creating Dialogs
Activity#removeDialog(int)

Andrew Burgess
Thank you, that was exactly what I was searching for.
Ulrich Scheller
The dialog only needs to be created once, for performance reasons (it's a relatively expensive operation). Rather than recreating it each time is is displayed, you can use the onPrepareDialog method to make changes to the dialog as it is displayed, as explained in the next answer.
ChrisV
+1  A: 

exactly. for AlertDialog, that was created w/ Builder.create(), onPrepareDialog() is useless. Builder is one-way in that once the dialog is created, you can't update. i mean can't loosely, i am sure you could get a handle to the view and do it all manually, but that defeats the point of using the builder in the first place.

the only solution i found was to manually create / show / dismiss the dialog instead of using onCreateDialog(), showDialog(), etc. i tried calling removeDialog(), but that did not seem to work.

Jeffrey Blattman
A: 

removeDialog(int) after you dismiss it.

tjlian616
A: 

This is a dup of this question: http://stackoverflow.com/questions/834139/android-can-not-change-the-text-appears-in-alertdialog

You can also do it this way: http://andmobidev.blogspot.com/2010/03/modifying-alert-dialogs-list-items.html

Does seem to slow down the display of the longpress menu though...

kenyee