Home > OS >  Looping array with while get only last value
Looping array with while get only last value

Time:09-22

I have three array

int[] image = {R.drawable.img1,R.drawable.img2}    int[] sound= {R.raw.m1,R.raw.m2}    String[] nom ={"el1","el2"}

I tryed to change a view with imageview, text from those array when click button but i got only the last value from three array these my method i call when onclick method

  private void updateData() {

       while (i<nom.length) {
           ImageView.setImageResource(image[i]);
           textview.setText(nom[i]);
           mysong = MediaPlayer.create(Activity.this, sound[i]);
           mysong.start();
           i  ;
       }

}

CodePudding user response:

Try this....

 Random random = new Random();
 private void updateData() {
       int random_number = random.nextInt(image.length);
       ImageView.setImageResource(image[random_number]);
       textview.setText(nom[random_number]);
       mysong = MediaPlayer.create(Activity.this, sound[random_number]);
       mysong.start();
  }

Note : Make Sure Your All Three Array Same Size...

CodePudding user response:

Try this....

int anInt = 0;

And After OnClick

 private void updateData() {
       if (anInt < image.length - 1) {
          anInt  ;
       } else {
          anInt = 0;
       }

       ImageView.setImageResource(image[anInt]);
       textview.setText(nom[anInt]);
       mysong = MediaPlayer.create(Activity.this, sound[anInt]);
       mysong.start();
  }

Note : Make Sure Your All Three Array Same Size...

  • Related