开发者

Android use of an string array on another method

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


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...


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;
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜