Looping array with while get only last value

Issue

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++;
       }

}

Solution

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…

Answered By – Jay Panchal

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published