Reputation: 35
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;
}
Upvotes: 0
Views: 23724
Reputation: 1
public ImageAdapter(Context c)
{
mContext = c;
myString = new String[12]; //create array
for (int number = 0; number < myString.length; number++) {
myString[number] = "image" + number;
}
}
Upvotes: 0
Reputation: 11535
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.
Upvotes: 4
Reputation: 93183
It should be:
String[] myString = new String[12];
for (int number = 0; number <= 12; number++) {
myString[number] = "image" + number;
}
Upvotes: 2