My app is getting a little bloated, I would like to ‘compress’ some code by changing some of my variables into arrays and using some for loops to process my variables.
My first target is initializing my EditText boxes
My original code (working fine) included:
EditText miBox1, spBox1 ;
EditText miBox2, spBox2 ;
…
miBox1 = (EditText)this.findViewById(R.id.miBox1);
spBox1 = (EditText)this.findViewById(R.id.spBox1);
miBox2 = (EditText)this.findViewById(R.id.miBox2);
spBox2 = (EditText)this.findViewById(R.id.spBox2);
I actually have twenty mi boxes and 20 sp boxes to setup
I am trying to create an array, and loop through them
In my main.xml I have renamed my EditText boxes to
miBox[1] spBox[1], etc.
I’m declaring in my activity with:
EditText[] miBox = new EditText[20] ;
EditText[] spBox = new EditText[20] ;
and putting this into a for loop
miBox[i] = (EditText)this.findViewById(R.id.miBox[i]);
spBox[i] = (EditText)this.findViewById(R.id.spBox[i]);
but Eclipse cannot resolve R.id.miBox[i]
How do I properly create an array of EditText in my xml so that
it is recognized?
Thanks
JD
where i is your index
if you want to put it in an array, simply do this :