tags:

views:

97

answers:

2

In the ImageAdapter class of this tutorial, http://developer.android.com/resources/tutorials/views/hello-gridview.html

I would like to create and populate an array using a for loop. But it seems no matter where I place it, it causes an error.

For example, under private Context mContext; I put in the following and it causes an error. I think the loop is good, I'm just not sure where I can put it.

private String[] myString; for (int number = 0; number <= 12; number++) { myString[number] = "image" + number; }

A: 

It should be:

String[] myString = new String[12];
for (int number = 0; number <= 12; number++) {
  myString[number] = "image" + number;
}
Macarse
This won't help if he's just going to paste it outside of any method or constructor :)
Nick
@Nick: ok, you are right. I just removed the word :)
Macarse
+2  A: 

Create and populate the array in the constructor. Don't forget to actually instantiate the array before you start populating it.

public ImageAdapter(Context c) {
    mContext = c;
    myString = new String[12]; //create array
    for (int number = 0; number < myString.length; number++) { 
        myString[number] = "image" + number; 
    }
}

You should perhaps work on your Java a bit before jumping straight into Android.

Nick