0

I want an array of imageviews, but I don't know how to fill it with an object of type imageview.

 ImageView[] forAdapter = new ImageView[imageIds.size()];
 for(int i = 0; i < imageIds.size(); i++)
 {
                    ImageView mImageView = new ImageView(context);
                    forAdapter[i] = mImageView.setImageDrawable(((imagesPulled.get(imageIds.get(i)))));
}

this doesn't work because .setImageDrawable does not return an imageview, and I dont know of any object that actually does!

I considered using drawables in an array, but I'm ultimately setting an arrayadapter for a listview, and I cant make a R.layout with drawables (I'll get a class cast exception because the xml file is using an ImageView not drawable type),so I only have ImageViews to put into an array - unless I'm approaching the problem wrong.

2 Answers 2

6

Your code is almost there! I'm just gonna make a small change and comment on it below:

 ImageView[] forAdapter = new ImageView[imageIds.size()];
 for(int i = 0; i < imageIds.size(); i++)
 {
                    forAdapter[i] = new ImageView(context);
                    forAdapter[i].setImageDrawable(imagesPulled.get(imageIds.get(i)));
}

First: If you want to initialize forAdapter[i], you don't need to create a new variable and assign it to it. Just do it there:

forAdapter[i] = new ImageView(context);

Setting the image (using setImageDrawable, setImageResource, etc) doesn't return anything. It's an operation you do on the image itself so all you gotta do is call the method from the variable you want to modify:

forAdapter[i].setImageDrawable(imagesPulled.get(imageIds.get(i)));

You're done :) If you have any doubts just ask in the comments.

Hope it helps.

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

1 Comment

thank you, yes I got it to work. Ended up changing things around a bit for a custom array adapter. I was trying to skip the custom array adapter despite its flexibility
2

Your mImageView is ImageView itself.

So, forAdapter[i] = mImageView should work fine. If you use additional actions, you can do them before assignment.

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.