Suppose I have an application that have similar buttons named button0, button1, etc, up to button9.
How can I do the following without duplicating code?
button0 = (Button) activity.findViewById(com.sample.SampleApp.R.id.button0);
button1 = (Button) activity.findViewById(com.sample.SampleApp.R.id.button1);
...
button9 = (Button) activity.findViewById(com.sample.SampleApp.R.id.button9);
I tried to use reflection but the code looks unclean.
for (int i = 0; i <= 9; i++) {
String btnName = "button" + i;
/* do reflection stuff to link self.buttonX
with a reference to com.sample.SampleApp.R.id.buttonX */
}
The following code is untested, but give it a try:
With ArrayList (again untested – just to give you an idea of what I mean):