Reputation: 255
I have a bunch of strings in strings.xml, all the id's/names of the strings have a pattern (e0,e1,e2,e3,e4...) I would like to display one of the 23 strings I have depending on what number the user chooses. For example, if the user chooses the number 6, than I would want to display the string "e6". How can I do that without using a super long switch statement?
I'm using IntelliJ Idea
Thank's for the Help, All your answers worked and were useful.
Upvotes: 0
Views: 133
Reputation: 1
Going off of Michael's example, I would do something like this:
Int num; //the number the user chooses
TextView tv = (TextView) findViewById(R.id.textviewID);
tv.setText(planets[num - 1]); //arrays start at zero so you would have to subtract one to get the right string. And also so you dont reference a nonexistent variable in the array.
Upvotes: 0
Reputation: 495
You can use a String Array
http://developer.android.com/guide/topics/resources/string-resource.html#StringArray
EXAMPLE:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
Upvotes: 7