简体   繁体   中英

changing imageview image inside a loop in android

i have some imageviews in my project with this id's: stagebtn_1,stagebtn_2,stagebtn_3 and...

by default all of them points to an image nad works perfectly... now i want to change picture of some of them inside in a loop with this code:

for (int i=1;i<=3;i++) {
        int imageViewId = getResources().getIdentifier("stagebtn_" + i, "id", "com.english.game");
        ImageView imageView = (ImageView) findViewById(imageViewId);
        imageView.setBackgroundResource(R.drawable.pic1);
    }

but it doesnt work in the way that i want, this code doesnt change pictures, instead creates 3 new imageview with new picture...

how i can solve this?

you can use this way

 public class MainActivity extends Activity {

 ImageView imageView;

 Integer[] image = { R.drawable.ic_launcher, R.drawable.tmp,R.drawable.android };

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.splash);

   imageView = (ImageView) findViewById(R.id.img);

   for(i=0;i<2;i++){
        imageView.setImageResource(image[i]);
     }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM