Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update array of items in an AlertDialog list built with AlertDialog.builder after creation

I have created a dialog that shows a multi-choice list of items that can be checked, using AlertDialog.builder.

I set the initial set of item names and their checked state thus:

builder.setMultiChoiceItems( saveTargets.names, saveTargets.checked, new DialogInterface.OnMultiChoiceClickListener() {

In my dialog I have added a button that creates a new item that should be shown and be selectable in the multi-choice list.

How can I ask the Dialog to update the list to show the new item?

I have it added to my "saveTargets" variables, but need toset the new data into the list view in the alert dialog.

I tried using a cursor to setup the multi-choice. I can't use that now for other reasons.

I've looked at getting the ListView and the Adaptor from the alert dialog, but can't see any calls to renew the array of names and checked status.

like image 822
Andrew Mackenzie Avatar asked Nov 14 '22 01:11

Andrew Mackenzie


1 Answers

I needed to do something like this as well. After looking on google, stackoverflow and the documentation, it looks like it relatively impossible to do without making your own adapter to handle the list (see: How to customize the list items in an Android AlertDialog). Since I only needed this for a single dialog, I ended up doing what the documentation says don't do: I made an alertdialog in my own method and did not make it part of "onCreateDialog" in my activity (I had to do this for a series of dialogs for another class in my app as well). This way the dialog is recreated from scratch each time it is called so the list is updated each time. That was the easiest fix I could find personally. Not as clean, maybe, but easier to add and works like it should.

like image 80
Christian Smith Avatar answered May 18 '23 14:05

Christian Smith