user2992340
user2992340

Reputation: 1

How to select a stringArray given an input

i'm learning how to do simple apps with Android.

i have a strings.xml with several array-strings. what i want is in an Android Activity to select automatically an array-string given an input string that will have the same name as an array-string in strings.xml.

my input is arraylist1, arraylist2 till arraylist20.

the names of my array-strings in my strings.xml are arraylist1, arraylist2 till arraylist20 each Array will have diferent strings.

for example i have an input my-string ="arraylist11". there is a way to refere "my-string" dinamically??

so if input is arraylist11 will get arraylist11 if the input is arraylist7 then get arraylist7 and so on.

how would be the code bellow?? "res.getStringArray(R.array.my-string)"

thanks

Upvotes: 0

Views: 59

Answers (1)

Apoorv
Apoorv

Reputation: 13520

Try using

getResources().getString(getResources().getIdentifier(yourStringName, "string", getPackageName()));

to get your String but this is not recommended.

As per Android Developer

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

Upvotes: 1

Related Questions