So I'm programming a simple calculator in Eclipse for Android 4.0 and I'm trying to stream-line my code and make it as simple as possible. The place I'm trying to clean up is my findViewById()'s. Since I have buttons 0-9 to instantiate I have a block of code ten lines long that look like this:
b0 = (Button) findViewById(R.id.b0);
b1 = (Button) findViewById(R.id.b1);
...
b9 = (Button) findViewById(R.id.b9);
As you can see this thing is just begging for a for-loop. So what I wanted to do was make two arrays. One instance variable array in the Activity which holds all the Button instance variables for the number pad:
private Button[] numberPad = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9};
and then another array in the id class in the R.java file that holds all the button id variables that would look like this:
private static final int[] numberPad = {b0,b1,b2,b3,b4,b5,b6,b7,b8,b9};
Sooo that I can reduce ten lines of Button instantiations to two using this loop:
for(int i = 0; i < numberPad.length; i++)
numberPad[i] = (Button) findViewById(R.id.numberPad[i]);
When I type it out it's fine, but when I go to save it it reverts back automatically to it's original form. I don't see anything wrong with this code. It doesn't produce any errors as far as I can tell. Why can't I edit the R.java file in this way? Is there a way to? Should I?