title: Android looping through an array of ImageView and calling a function that takes the ImageView as argument
body: <p>I want to loop through an array of ImageView(s) and call a function for each ImageView and using the ImageView as the argument for the function, and also set a pause for the loop, so that the loop will pause for 3s before calling the function again with the next ImageView. So far this is the closest that I got, but it still showed up error as I am unable to access the array from within the Runnable. </p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends Activity implements View.OnClickListener {
  
      ImageView imageView1, imageView2, imageView3,........ imageView16;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          imageView1 = (ImageView) findViewById(R.id.imageView1);
          imageView2 = (ImageView) findViewById(R.id.imageView2);
          .
          .
          .
  
          Handler handler = new Handler();
  
          // Array of ImageView(s)
          final ImageView[] pieces = {imageView1, imageView2, .....};
  
          // Looping through ImageView array
          for(int i = 0; i &lt; pieces.length; i++) {
              Runnable shuffle = new Runnable() {
                  public void run() {
                      playLevel(pieces[i]);
                  }
              };
              // delay the calling of function so that the loop pause every 3s
              handler.postDelayed(shuffle, 3000);
          }
      }
  
      public void playLevel(ImageView imageView) {
          // some function
      }
  }
  </code></pre>
  
  <p>All suggestions and better way of achieving this are greatly welcomed. Thank you.</p>