Android - 通过 getIdentifier() 和 getStringArray 使用另一个字符串引用字符串数组

2023-11-23

我想要访问多种字符串数组,具体取决于用户决定使用哪一个。我不想使用 SQLite DB,因为我对 Android/Java 很陌生,而且我一直在努力寻找示例,所以我猜这是一种相当糟糕的方法,但无论如何......

如果我的 xml 文件中有这样的内容:

    <string-array name="bob">
<item>1</item>
<item>4</item>
<item>7</item>
<item>11</item>
</string-array>

在 Java 文件中:

String name = "bob";

为什么以下不起作用?每次启动就崩溃。

int holderint = getResources().getIdentifier("name", "array",
                this.getPackageName());
String[] items = getResources().getStringArray(holderint);

这条线不应该是这样的吗?

int holderint = getResources().getIdentifier(name, "array",
                this.getPackageName()); // You had used "name"
String[] items = getResources().getStringArray(holderint);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android - 通过 getIdentifier() 和 getStringArray 使用另一个字符串引用字符串数组 的相关文章

随机推荐