I know, that Java doesn't have a pre-processor, so some stuff is more or less impossible in java.
Is there really NO way to fill those arrays with dynamic names in a loop? I'd like to have something like:
for(int i=0;i<5;i++){
earnTvs[i]=(TextView) findViewById(R.id.INSERT_GREAT_TRICK_HERE("earn"+i+"Tv"));
}
instead of
earnTvs[0] = (TextView) findViewById(R.id.earn1Tv);
earnTvs[1] = (TextView) findViewById(R.id.earn2Tv);
earnTvs[2] = (TextView) findViewById(R.id.earn3Tv);
earnTvs[3] = (TextView) findViewById(R.id.earn4Tv);
earnTvs[4] = (TextView) findViewById(R.id.earn5Tv);
timeTvs[0] = (TextView) findViewById(R.id.time1Tv);
...
ownTvs[0] = (TextView) findViewById(R.id.own1Tv);
...
costTvs[0] = (TextView) findViewById(R.id.build1Tv);
...
buyBtns[0] = (ImageButton) findViewById(R.id.buy1Bt);
...
progressBars[0] = (ProgressBar) findViewById(R.id.prog1pB);
...
buildBtns[0] = (Button) findViewById(R.id.build1Bt);
...
Or is there any kinky trick, that can be used?
You could use reflection for this purpose, but that's known to be slow on Android (especially older versions) and for the specified use case pretty unmaintainable; what if the ID's change? You'd have to make your changes in 2 places (XML & code) rather than let your IDE handle propagating the change.
If it's boilerplate you're annoyed with, perhaps you should take a look at Android Annotations. It uses compile-time annotations & an annotation processor to help you reduce boilerplate code.
With reflection. But I'm not sure if reflections are good here...
I would do it like that:
If you want to use the
getIdentifier()
method: