I have this piece of code that aims to translate a string from a Spinner item to an integer for use in another part of the application. This worked well when I had constant strings in the switch case, but later on I wanted to move those hard-coded strings into the Strings.xml resource to separate them from the logic. This is where I wound up in trouble, since Java wants the strings to be constant.
I tried to make the strings final, but it didn't make any difference. So my question is, would it somehow be possible to make the strings from a resource constant to make them usable in a switch case like the one in the code snippet below?
public int getPositionFromText(String text) {
// The string representations of the different score methods.
String[] scoreOptions = getResources().getStringArray(R.array.scoreOptions);
final String three = scoreOptions[0];
final String four = scoreOptions[1];
final String five = scoreOptions[2];
switch(text) {
case three:
return 0;
case four:
return 1;
case five:
return 2;
default:
return 0;
}
}