0

I have a Spinner on my Activity with items "hello" and "goodbye" for user to choose. These items, when device is in Spanish, would be "hola" and "adios", as strings are chosen from string-es/ instead from string.

The problem is that I've got many switch cases on my code like this:

switch(spinnerSelectedItem){
     case "hello":
     case "hola":
          //do something
          break;
     case "goodbye":
     case "adios":
          //do something else
          break;

}

I've tried to create a final String initialized with string resource, but it complains saying "Constant Expression Required"

final String resHello = getResources().getString(R.string.helloText);
final String resGoodbye = getResources().getString(R.string.goodbyeText);

switch(spinnerSelectedItem){
     case resHello :    
          //do something
          break;
     case resGoodbye :
          //do something else
          break;
}

This is a simplified version, I have many more items and more than two languages

One solution would be to use strings id instead of value, or use if clauses instead of switch, but I would like to find a "smart" or "clean" way to do this, do you have any idea?

musica
  • 1,373
  • 3
  • 15
  • 34
jordileft
  • 39
  • 6
  • Using the ids would be better since you would be able to use them in switch cases and they would point to the correct resource automatically depending on the language – ashkhn Jan 10 '17 at 10:28
  • Then I should write a method for translating from selected string to id, right? Like this for example: `public int idFromStringResourceValue(String resValue){ if(resValue.equals(getResources(). getString(R.string.helloText)){return R.string.helloText;} if(resValue.equals(getResources(). getString(R.string.goodbyeText)){return R.string.goodbyeText;} }` Or there is a better way? – jordileft Jan 10 '17 at 10:47
  • Switch statement on String objects is a new feature introduced in Java 1.7 http://stackoverflow.com/questions/14367629/android-coding-with-switch-string – Suhayl SH Jan 10 '17 at 10:53
  • Yes, I can use switch with strings but it only works if I hardcode the string in the switch case, if it's not hardcoded it gives me "constant expression required" when using resource string value even as a final string, like the second code in the original question. Am I doing something wrong? – jordileft Jan 10 '17 at 11:02

1 Answers1

0

You can use the getIdentifier method of Resources to get the id of the string. eg: getResources().getIdentifier(resValue, "string", getPackageName()) This will return the id which you can then use in your switch statements and compare.

ashkhn
  • 1,642
  • 1
  • 13
  • 18
  • Ok thanks! I think that I'll write my own method for just looking for a match in a limited range of strings related to the spinner, probably would be faster than using this method would look for through all the string resources. – jordileft Jan 10 '17 at 11:29
  • It depends on the number of string resources you have. But I think this method will be more extensible in the future as you keep adding strings resources in multiple languages – ashkhn Jan 10 '17 at 13:18
  • Thank you so much! – jordileft Jan 10 '17 at 13:53