Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have many string arrays in my resource files, and I want to access them programmatically depending on user input.

int c = Getter.getCurrentNumber();
String[] info = getResources().getStringArray(R.array.n_<c>);

So if c==12, info should be the string-array with name "n_12". Is there a way to do this, and avoiding to do a switch statement with hundreds of cases?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
112 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can get the resource id like so

int c = Getter.getCurrentNumber();
String resource = "n_" + c;
int id = getResources().getIdentifier(resource, "array", "com.your.project");

Then just use that id

String[] info = getResources().getStringArray(id);

Have a look here for another example on getResources().getIdentifier().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...