vendredi 25 mars 2016

How to shorten if x then y in java

I am trying to upload multiple images into 4 image views, I would like to use a for loop to do it.

ImageView view = defaultView;
String url = {"jpg1.png", "jpg2.png", "jpg3.png", "jpg4.png"};

    for (int i = 0; i < 4; i++) {
        if (i==0) {
            view = aView;
        }
        if (i==1) {
            view = bView;
        }
        if (i==2) {
            view = cView;
        }
        if (i==3) {
            view = dView;
        }

        Glide
                .with(this)
                .load("Website/folder/" + url[i])
                .override(widthpx, heigthpx)
                .into(view);

How could I use less if statements? Is this possible? Or is there a way to store variables inside an array? (like: Variables[] view = {default, aView, bView, cView, dView}

Thanks!

Aucun commentaire:

Enregistrer un commentaire