In my app I have numerous strings used to identify a region and location.:
<string name="r1s1">Seattle</string>
<string name="r1s2">Des Moines</string>
<string name="r1s3">Bremerton</string>
<string name="r2s1">Tacoma</string>
<string name="r2s2">Burton</string> etc, etc.
I can readily access these strings by string name by using a long switch statement:
switch(regionCode
{ case 1: sN=getString(R.string.r1s1);break;
case 2: sN=getString(R.string.r2s1);break; etc etc
I want to calculate this string name and use it to access the string more directly:
stationNameKey="r"+regionCode+"s"+stationCode;// e. g. r1s1
stationName=getString(R.string.stationNameKey);
This second statement is not correct and will not compile here is the message:
! compiler error:: stationNameKey cannot be resolved or is not a field.
I assume a cast from String to Resource Name Identifier would work but cannot find it.
Q: How do I cast a string to make my second statement work correctly. Thank You.
You should use Resources.getIdentifier()
see this and this
should work to get the id you need