How to update an array of items in an AlertDialog list created using AlertDialog.builder after creating

I created a dialog box that displays a list of items that can be checked using AlertDialog.builder.

I set the initial set of element names and their check state, thus:

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

In my dialog box, I added a button that creates a new item that needs to be shown and can be selected in a list with multiple selections.

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

I added it to my saveTargets variables, but you need to configure the new data as a list in the alerts dialog.

I tried using the cursor to set up multiple selections. I cannot use this for other reasons.

I looked at getting the ListView and adapter from the alert dialog, but I see no calls to update the array of names and check the status.

+4
source share
1 answer

I also needed to do something similar. Looking at google, stackoverflow, and the documentation, it seems like it's relatively impossible to prevent your own adapter from processing the list (see How to configure list items in Android AlertDialog ). Since I only need this for one dialog box, I ended up working on what the documentation says: I made alertdialog in my own method and did not make it part of the “onCreateDialog” in my activity (I had to do this for a series of dialogs for another class in my application). Thus, the dialog is recreated from scratch every time it is called, so the list is updated every time. It was the easiest solution that I could find personally. Not so clean, maybe, but it's easier to add and work as it should.

+1
source

Source: https://habr.com/ru/post/1346570/


All Articles