0

Here is the list of custom-made rounded images. need to dynamically assign a value through the loop.

            RoundedImageView img23 = (RoundedImageView) findViewById(R.id.avatar23);
            img23.setImageResource(R.mipmap.avatars_male_28);

            RoundedImageView img24 = (RoundedImageView) findViewById(R.id.avatar24);
            img24.setImageResource(R.mipmap.avatars_male_29);

            RoundedImageView img25 = (RoundedImageView) findViewById(R.id.avatar25);
            img25.setImageResource(R.mipmap.avatars_male_30);

            for (i = 1;i>25; i++){
                j=(String) i;

                //need a loop that dynamically sets img**2** (any number)
                img<j>.setImageResource(R.mipmap.avatars_male_30);
            }
2
  • 4
    add the Images to a list or Array? Commented Aug 25, 2016 at 9:44
  • create a list List<RoundedImageView> and then iterate your list. easy,fast :) Commented Aug 25, 2016 at 10:07

3 Answers 3

2

Instead of creating RoundedImageView img1, RoundedImageView img2, RoundedImageView img3 ...

just create an ArrayList of RoundImageView:

ArrayList<RoundedImageView> list = new ArrayList<RoundedImageView>();
list.add((RoundedImageView) findViewById(R.id.avatar1));
list.add((RoundedImageView) findViewById(R.id.avatar2));
list.add((RoundedImageView) findViewById(R.id.avatar3));

. . .

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

Comments

0

There you can do by making imageview type model and then extract imageview from model and use your drawables into imageview Like this...

    //You can make ImageViewModel type class there you save your image view
     class ImageViewModel {

        public ImageView getImage() {
            return image;
        }

        public void setImage(ImageView image) {
            this.image = image;
        }

        ImageView image;

    }
    ArrayList<ImageViewModel> images = new ArrayList<>();
    ArrayList<Integer> drawables = new ArrayList<>();
    drawables.add(R.mipmap.ic_launcher);
    drawables.add(R.mipmap.ic_launcher);
    drawables.add(R.mipmap.ic_launcher);



    //set you imageview in model class and add into the arraylist of modelclass type
    ImageViewModel model = new ImageViewModel();
    model.setImage(imageviews);
    images.add(model);




   // Then you can set this as
    for(int i = 0; i<drawables.size(); i++){

         model.getImage().setImageResource(drawables.get(i));
    }

Hence You can set your images into Imageview dynamically

This worked for me may be for you also

Comments

0

To remove all this boilerplate code use butterknife library http://jakewharton.github.io/butterknife/

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.