Issue
Using java for Android, I have come across a problem:
for(int a = 0; a < 26; a++){
textViewArray[a] = (TextView) findViewById(R.id.(alphabet[a]));
}
I have 26 TextViews in an xml file with the ids A, B, C, D, ...Z I need the above to become R.id.A, R.id.B, R.id.C, ...R.id.Z when it is run. The above is one of my attempts which for obvious reasons doesn't work. I have thought of using
int[26] = new int[]{R.id.A, R.id.B, R.id.C .... R.id.Z};
then using those array entries in the above for loop but is there a neater way to do it? Possibly converting a string ( "R.id." + (alphabet[a]) ) to code and letting it run as usual to find the id?
P.S. alphabet is a String array containing A, B, C ... Z in the above example
Solution
Resources.getIdentifier(). Keep in mind that this function is quite slow, so don't use it unless you have to.
In your case, I'd stick with the array you proposed, that's the better approach.
Answered By - EboMike
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.