skip to Main Content

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

}

2

Answers


  1. 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…

    Login or Signup to reply.
  2. 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…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search