How to implement setMultiChoiceItems element values ​​in onPrepareDialog?

I am showing a checkbox dialog (a list retrieved from the database) to allow the user to choose which rows to delete. Since caching is in the Android dialog, I need to update the count and flag names. 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 to update values ​​(items, _selections) in onPrepareDialog? I tried to invalidate the views, hoping that force android would load the items into the againe (none work), but I think this is a bad choice, as well as deleting the dialog and re-creating.

 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!

+7
source share
4 answers

When you create a list of items using AlertDialog.Builder , it internally accepts this and creates a ListAdapater , which depends on the type of data transferred. Since the "elements" in your example are not similar to the resource identifier, I assume that this is either CharSequence [] or Cursor. If you provide more detailed information on what β€œelements” are, I can give a more concrete example.

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

You will need to get a link to this ListAdapter using getListView().getAdapter() in an instance of AlertDialog.

For the cursor, you can avoid calling notifyDataSetChanged() after you requery() to update the dataset.

Since you cannot "update" the array with new data (changing the pointer to a new instance is not the same ... the instance that the adapter points to remains unchanged), this case is a bit more work. You will need to call add() , clear() , etc. An adapter to remove invalid items and add updates. When the adapter dataset is fully updated, you can now call notifyDataSetChanged() .

Hope this helps!

+6
source

I spent a lot of time looking for the same solution and ended up fixing my problem with simple things, trying to use onPrepareDialog too

I am using the removeDialog (int) function for an Activity. When a dialog is rejected, activity basically keeps the state of the dialog box (for performance reasons that I would have imagined). Calling removeDialog (int) in the dialog box causes the action to unload all links for the dialog and rejects it from the screen if it is displayed.

did this when my activity lost focus, just add:

  public void onStop() { removeDialog(Id_Dial); return; } 
+2
source

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; } } 
+1
source

In your onPrepareDialog method, instead of using invalidateViews (), you should try to get the ListView adapter and try to call either notifyDataSetChanged () or notifyDataSetInvalidated (). Does it help?

0
source

All Articles