title: How to get values from a Arraylist to Buttons?
body: <p>I'm trying to get random values from a <code>Arraylist</code> and then storing it in <code>Buttons</code>.
  Every time when I run this code with the buttonListRbra my app crashes.</p>
  
  <p><strong>Here is my code:</strong></p>
  
  <pre><code>    private void generateRandomNumbersRbra()
   {
    int rnd;
    Random rand = new Random();
    int[] randomPick = new int[8];
    ArrayList&lt;Integer&gt; numbersRbra = new ArrayList&lt;Integer&gt;();      
    Button[] buttonListRbra = new Button[8];
    buttonListRbra[0] = (Button)findViewById(R.id.tap1);
    buttonListRbra[1] = (Button)findViewById(R.id.tap2);
    buttonListRbra[2] = (Button)findViewById(R.id.tap3);
    buttonListRbra[3] = (Button)findViewById(R.id.tap4);
    buttonListRbra[4] = (Button)findViewById(R.id.tap5);
    buttonListRbra[5] = (Button)findViewById(R.id.tap6);
    buttonListRbra[6] = (Button)findViewById(R.id.tap7);
    buttonListRbra[7] = (Button)findViewById(R.id.tap8);
    buttonListRbra[8] = (Button)findViewById(R.id.tap9);    
    for (int i=0 ; i&lt; 8;i++)
    {
     rnd = rand.nextInt(8) + 1;
  
     if(i==0)
     {
      randomPick[0] = rnd;
      numbersRbra.add(randomPick[0]);
     }
     else
     {
      while(numbersRbra.contains(new Integer(rnd)))
      {
       rnd = rand.nextInt(8) + 1;
      }
      randomPick[i] = rnd;
      numbersRbra.add(randomPick[i]);
     }
    }
   }
  </code></pre>
  
  <p>logCat:</p>
  
  <blockquote>
    <p>05-07 14:15:50.325: D/libEGL(10749): loaded /system/lib/egl/libEGL_mali.so
    05-07 14:15:50.355: D/libEGL(10749): loaded /system/lib/egl/libGLESv1_CM_mali.so
    05-07 14:15:50.360: D/libEGL(10749): loaded /system/lib/egl/libGLESv2_mali.so
    05-07 14:15:50.370: E/(10749): Device driver API match
    05-07 14:15:50.370: E/(10749): Device driver API version: 23
    05-07 14:15:50.370: E/(10749): User space API version: 23 
    05-07 14:15:50.370: E/(10749): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct  9 21:05:57 KST 2013 
    05-07 14:15:50.490: D/OpenGLRenderer(10749): Enabling debug mode 0
    05-07 14:15:54.010: D/AndroidRuntime(10749): Shutting down VM
    05-07 14:15:54.010: W/dalvikvm(10749): threadid=1: thread exiting with uncaught exception (group=0x41bdc700)
    05-07 14:15:54.015: E/AndroidRuntime(10749): FATAL EXCEPTION: main
    05-07 14:15:54.015: E/AndroidRuntime(10749): java.lang.IllegalStateException: Could not execute method of the activity
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at android.view.View$1.onClick(View.java:3838)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at android.view.View.performClick(View.java:4475)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at android.view.View$PerformClick.run(View.java:18786)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at android.os.Handler.handleCallback(Handler.java:730)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at android.os.Handler.dispatchMessage(Handler.java:92)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at android.os.Looper.loop(Looper.java:176)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at android.app.ActivityThread.main(ActivityThread.java:5419)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at java.lang.reflect.Method.invokeNative(Native Method)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at java.lang.reflect.Method.invoke(Method.java:525)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at dalvik.system.NativeStart.main(Native Method)
    05-07 14:15:54.015: E/AndroidRuntime(10749): Caused by: java.lang.reflect.InvocationTargetException
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at java.lang.reflect.Method.invokeNative(Native Method)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at java.lang.reflect.Method.invoke(Method.java:525)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at android.view.View$1.onClick(View.java:3833)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    ... 11 more
    05-07 14:15:54.015: E/AndroidRuntime(10749): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at com.roc.quickynumbers.GameActivity.generateRandomNumbersRbra(GameActivity.java:73)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    at com.roc.quickynumbers.GameActivity.startGame(GameActivity.java:46)
    05-07 14:15:54.015: E/AndroidRuntime(10749):    ... 14 more</p>
  </blockquote>
  
  <p>Thanks in advance!</p>
  
  <p>Edit:</p>
  
  <p>Thanks everyone. I got it working now!</p>
  
  <p>Solution:</p>
  
  <pre><code>int rnd;
      Random rand = new Random();
      ArrayList&lt;Integer&gt; numbersRbra = new ArrayList&lt;Integer&gt;();
  
      Button[] buttonListRbra = new Button[9];
      buttonListRbra[0] = (Button)findViewById(R.id.tap1);
      buttonListRbra[1] = (Button)findViewById(R.id.tap2);
      buttonListRbra[2] = (Button)findViewById(R.id.tap3);
      buttonListRbra[3] = (Button)findViewById(R.id.tap4);
      buttonListRbra[4] = (Button)findViewById(R.id.tap5);
      buttonListRbra[5] = (Button)findViewById(R.id.tap6);
      buttonListRbra[6] = (Button)findViewById(R.id.tap7);
      buttonListRbra[7] = (Button)findViewById(R.id.tap8);
      buttonListRbra[8] = (Button)findViewById(R.id.tap9);
  
      for (int i=0; i &lt; 9; i++) // Loop through your entire list to access all your 9 Buttons
      {
          rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9] =&gt; 9 elements
  
          if(i==0)
          {
             numbersRbra.add(rnd);
             buttonListRbra[i].setText(numbersRbra.get(i).toString()); //Set the Button Text for the first iteration too
          }
          else
          {
            while(numbersRbra.contains(rnd)) // Transformation between Integer and int done automatically
            {
                rnd = rand.nextInt(9) + 1; // Return an int in the range [1,9]
            }
            numbersRbra.add(rnd);
            buttonListRbra[i].setText(numbersRbra.get(i).toString()); // Set it after finding the correct number
          }
        }
  </code></pre>