0

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

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

It should be:

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

1 Comment

This won't help if he's just going to paste it outside of any method or constructor :)
0
public ImageAdapter(Context c)
 {
    mContext = c;

    myString = new String[12]; //create array


   for (int number = 0; number < myString.length; number++) { 

        myString[number] = "image" + number; 
    }
}

1 Comment

Please give at least a small explanation of your code. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.