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!
When you create a list of items using
AlertDialog.Builder, it internally takes that and creates aListAdapaterthat 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.CharSequence[](like String[]) data,Buildercreates an ArrayAdapter instance.Cursordata,Buildercreates a CursorAdapterYou 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 calledrequery()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 callnotifyDataSetChanged().Hope that Helps!