0

My problem is this. I have a number of ImageView objects in my XML layout. I want to be able to put images in to them deppending on some "if's" and "else's" (so to speak). So I thought I'd assign all those objects into an array. But the app wont start. Here is how I have done it:

private void setImageView(){
    images = new ImageView[imagesNR];
    images[0] = (ImageView) findViewById(R.id.imageView1);
    images[1] = (ImageView) findViewById(R.id.imageView2);
    images[2] = (ImageView) findViewById(R.id.ImageView01);
    images[3] = (ImageView) findViewById(R.id.ImageView02);
    images[4] = (ImageView) findViewById(R.id.ImageView03);
    images[5] = (ImageView) findViewById(R.id.ImageView04);
    images[6] = (ImageView) findViewById(R.id.ImageView05);
    images[7] = (ImageView) findViewById(R.id.ImageView06);
    images[8] = (ImageView) findViewById(R.id.ImageView07);
    images[9] = (ImageView) findViewById(R.id.ImageView08);
    images[10] = (ImageView) findViewById(R.id.ImageView09);
    images[11] = (ImageView) findViewById(R.id.ImageView10);
}

I realize that's it's not the preatiest way, but since I'm new, that's what I came up with. I'm affraid I can't tell you too much about the error. The app is being forced closed and that's it.

Is there any other way I can have an array of ImageView and manipulate them accordingly?... I want to put the objects into an RelativeLayout and set where to put all those ImageViews etc, etc.

Edit:

private ImageView images[];

That's how I declared the array.

1
  • Paste the stack trace from your for close. Commented Jan 7, 2012 at 14:03

3 Answers 3

1

Is your imageView defined like the Below (check for the brackets). This is a definition out of one of my apps and works definitly.

ImageView mCnName[]= new ImageView[NUMBER_OF_IMAGE];

The way is basically not wrong but if all ImageView should have similar or same attributes then I would use a little diffident approach. Lets you want by default all image view with a blue background. Like you do you would have to either in your layout.xml file give every of those the attribute or you would have to run a loop in order to change the bg color of every ImageView programatically.

In nearly all my apps I prefere the approach like this. Define the ImageView with all attribute in you xml file.

  1. define a layoutinflater and use mLayOutInflater=this.getLayoutInflater();
  2. mCnName[i]=mLayOutInflater.inflate(R.layout.yourImageView, null);
  3. add the inflated view with parentLayout.addView(mCnName[i],null); Like this it is easier to design and change your layout later.

But as I said it is not absolutly necessary to use my approach. Yours can work.

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

2 Comments

Will have to try out your way. I think it will take me a while before I'll get it...
OMG, I'm stupid.... I didn't set the variable imagesNR... It was undifined. Just had to set imagesNR = 12 and all is well..... OMG
0

I know you say all is well, but just a suggestion. This will benefit you a lot later if you take the time to get comfortable with this model now. You should know that multiple imageviews are more expensive than a composite one. If your looking to display an array of images, I suggest you use an implementation that was designed for just that ie; Gallery, GridView or ListView come to mind.

1 Comment

I'm not saying, that "my" way is the best, I will definetly look in to "your" way. I'll leave this for now, since it works and I don't have the time at the moment. The plan is after I'm done with "lame" version will try to improve it.... Make it more... "dynamic" and "pro" so to speak. Thanks for the tip, though.
0

1.

    for (int i = 0; i < container.getChildCount(); i++) {
        images[i] = (ImageView) container.getChildAt(i);
    }

2.

for (int i = 0; i < 10; i++) {
        int id = getResources().getIdentifier("R.id.imageView" + i, "id", null);
        images[i] = (ImageView) getActivity().findViewById(id);
}

Second one is quite expensive since per the doc of getIdentifier:

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

Comments

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.