0

I need some idea of how randomaly give each button[i] on of the values R.id.buttonj_mg. (one to one function...). I don't know how to do it since R.id.button1_mg is not a string, so I can't do somethink like R.id.button+j+_mg when j chossen randomaly..

This is the situation now:

    button[1]=  (Button)findViewById(R.id.button1_mg);
    button[2]=  (Button)findViewById(R.id.button2_mg);
    button[3]=  (Button)findViewById(R.id.button3_mg);
    button[4]=  (Button)findViewById(R.id.button4_mg);
    button[5]=  (Button)findViewById(R.id.button5_mg);
    button[6]=  (Button)findViewById(R.id.button6_mg);
    button[7]=  (Button)findViewById(R.id.button7_mg);
    button[8]=  (Button)findViewById(R.id.button8_mg);
    button[9]=  (Button)findViewById(R.id.button9_mg);
    button[10]=  (Button)findViewById(R.id.button10_mg);
    button[11]=  (Button)findViewById(R.id.button11_mg);
    button[12]=  (Button)findViewById(R.id.button12_mg);
    button[13]=  (Button)findViewById(R.id.button13_mg);
    button[14]=  (Button)findViewById(R.id.button14_mg); 
    button[15]=  (Button)findViewById(R.id.button15_mg);
    button[16]=  (Button)findViewById(R.id.button16_mg);
GO VEGAN
  • 1,113
  • 3
  • 15
  • 44

2 Answers2

0

You could use a collection to store your ints as Integers and then use the Java Collection class shuffle() method on those objects. Then you could remove them one by one from the Collection in each one of your buttons.

List<Integer> resources = new ArrayList<Integer>();
...
resources.add(R.id.button1);
...
Collections.shuffle(resources);
Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
0

One solution is to create the buttons and their ids in the code instead of taking them from resources, look at https://stackoverflow.com/a/11615356/987358. Then you can store them easily in a collection as another answer suggests.

Another solution is the Java reflection API which allows to retrieve the values of the ids using strings of the id names.

Community
  • 1
  • 1
Michael Butscher
  • 10,028
  • 4
  • 24
  • 25