views:

249

answers:

3

Hi all. Im trying to make an activity that has a multiple choice dialog after you push a button. In there you select from a list of things. But these things are received from a web method before the dialog appears.

So I create a string array after I receive them inside the onCreate to initialise it there with the correct size. But my dialog method then cant get the array because propably its out of its scope. My code looks like this

@Override
 protected Dialog onCreateDialog(int id)
//Here is where the array is loaded to the multiple select dialog
etc


@Override
public void onCreate(Bundle savedInstanceState)
//Here is where i initialise the array and get its contents
etc

I cant initialise my array when the class starts because I dont know its size yet. This has to do something with the scopes of my variables and I am pretty confused

A: 

if you use showDialog and want activity managed dialogs (you should do this) then you must implement onCreateDialog to actually create the dialog. This will get called once for each dialog you have. onPrepareDialog gets called every time you call showDialog(). So the code to update the dialog showing the string array should go in onPrepareDialog and the code to create the dialog should go in onCreateDialog.

public Dialog onCreateDialog(int id) {
    switch(id) {
        case MY_DIALOG:
            Dialog d = new Dialog(this);
            return d;
    }
}

public void onPrepareDialog(Dialog d, int id) {
    switch(id) {
        case MY_DIALOG:
            d.setSomeStringArray();
            break;
    }

}
BrennaSoft
A: 

So I should add the onPrepareDialog and erase the onCreateDialog or should both remain and have the same code inside them?

Thanks for the reply

spagi
i updated my answer with an example.
BrennaSoft
A: 

make the string array a class member, just populate it in onCreate. You can load it into the dialog in onCreateDialog if the array never changes, if it may change between calls to the dialog then you should do it in onPrepareDialog.

So in your class define:

private String mDialogStrings[];

then in onCreate something like:

mDialogStrings = new String[numItems];
mDialogStrings[0] = string1;
etc...
Mark