访问资源名称编程 [英] Access a resource name programmatically

查看:87
本文介绍了访问资源名称编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的资源文件中的许多字符串数组,我想以编程方式访问它们依赖于用户输入。

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>);

所以,如果c == 12,信息应该是字符串数组名称为n_12。 有没有办法做到这一点,并避免做一个switch语句有几百个案例?

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?

感谢

推荐答案

您可以得到的资源ID像这样

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");

然后,只需使用该ID

Then just use that id

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

看一看这里另一个例子是关于 getResources()。则getIdentifier ()

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

这篇关于访问资源名称编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆