Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How actualize setMultiChoiceItems items values in onPrepareDialog?

Tags:

android

dialog

I show dialog of checkboxes (list retrieved from DB) to allow user select, which rows remove. Because android dialog caching, I need to refresh count and names of checkboxes. In my onCreateDialog:

dialog =  new AlertDialog.Builder( this )
       .setTitle( "Remove Items" )
       .setMultiChoiceItems( items, _selections, new OnMultiChoiceClickListener(){public void onClick (DialogInterface dialog, int which, boolean isChecked){}} )
       .setPositiveButton("Smazat", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) { 
            dialog.dismiss(); 
            deleteRow(_selections);
            } })
        .setNegativeButton("Storno", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) { 
            dialog.dismiss(); 
            } })
       .create();

How refresh values (items,_selections) in onPrepareDialog? I tried invalidate views, hoping that force android to load items againg(dont work neither), but I think its bad choice as well as removing dialog and recreating.

protected void onPrepareDialog(final int id, final Dialog dialog) {
          switch (id) {
          case REMOVE_DIALOG_ID:

              ListView lv = ((AlertDialog) dialog).getListView();
                lv.invalidateViews();

          break;
          }

Thanks for any ideas!

like image 651
Hruskozrout Avatar asked Feb 13 '11 12:02

Hruskozrout


3 Answers

When you create a list of items using AlertDialog.Builder, it internally takes that and creates a ListAdapater that is dependent on the type of data you passed. Since "items" in your example doesn't look like a resource ID, I'm assuming it's either a CharSequence[] or a Cursor. If you provide more information about what "items" is, I can provide a more concrete example.

  • For CharSequence[] (like String[]) data, Builder creates an ArrayAdapter instance.
  • For Cursor data, Builder creates a CursorAdapter

You will need to obtain a reference to this ListAdapter using getListView().getAdapter() on the AlertDialog instance.

For a Cursor, you can get away with calling notifyDataSetChanged() after you have called requery() to update the data set.

Since you can't "update" an array with new data (changing the pointer to a new instance is not the same thing...the instance that the adapter is pointing to stays unchanged), this case is a little more work. You will need to call the add(), clear(), etc. methods of the adapter to remove invalid items and add the updates ones. With the adapters data set fully updated, you may now call notifyDataSetChanged().

Hope that Helps!

like image 77
devunwired Avatar answered Sep 23 '22 17:09

devunwired


I spent lot of time to search for same solution and eventually fixed my problem with simple stuff after trying to use onPrepareDialog too

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

did this when my activity lost focus simply add:

 public void onStop() {
            removeDialog(Id_Dial);
            return;
        }
like image 37
lejallec Avatar answered Sep 25 '22 17:09

lejallec


My technique is to create an adapter with empty data in onCreateDialog and completely replace the adapter during onPreparDialog.

Example:

@Override
protected Dialog onCreateDialog(int id) {

        switch (id) {

        case DialogMergeRegion:

            title = ...

            return new AlertDialog.Builder(BaseDataTabView.this)
                       .setTitle(title)
                       .setMultiChoiceItems(new CharSequence[0], null,
                                            new OnMultiChoiceClickListener() {

                                                @Override
                                                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                                    manageSelectionList(which, isChecked);
                                                }
                                            })
                      //...
                      .create();
        }
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);

    switch (id) {


        case DialogMergeRegion: {

            List<String> regionNames = ...// get the data
            ListAdapter mergeAdapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_multichoice, regionNames);

            AlertDialog ad = (AlertDialog) dialog;
            ad.getListView().setAdapter(mergeAdapter);

            break;
        }
}
like image 35
Timores Avatar answered Sep 23 '22 17:09

Timores