0

Scenario:

In strings.xml I have a list of city names with each their name in lowercase, for example:

<string name="london">London</string>

In my activity I have an array of model objects and each model object has a String variable named cityName which has the same name as its respective city name in strings.xml. For example:

   City {
      private String cityName;
      private String countryName;
   }

I want to be able to retrieve the string for each model object by passing the cityName when I call the string resource. I know this would be illegal but something like this:

City city1 = new City("london", "england");
R.string.city1.getCityName();

I know R.string keeps all the variables as constants but is there any way to map a string to the name of the constant in R.strings?

nicoqueijo
  • 872
  • 2
  • 11
  • 28
  • it is possible via reflexion https://code.tutsplus.com/tutorials/learn-java-for-android-development-reflection-basics--mobile-3203 – Iłya Bursov Jul 18 '17 at 22:42
  • This solution from the thread linked by Daniel Nugent worked https://stackoverflow.com/a/11595723/5906793 – nicoqueijo Jul 18 '17 at 23:14

0 Answers0