Android 获取字符串问题
Android getString issue
所以我在strings.xml里面有很多字符串,它们的记录格式是:
<string name="list_1">xxxxxxxxxxx</string>
<string name="list_2">xxxxxxxxxxx</string>
<string name="list_3">xxxxxxxxxxx</string>
......
现在我想一个一个地加载它们,而不必键入所有的字符串 ID。我想以这样的方式加载它们:
for (int i = 1; i <= num; i++) {
// Just showing what I mean.
String xxx = getString(R.string.("list_" + i));
}
有方法吗?
值得使用资源数组。例如
<?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>
即可访问,迭代如下:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
试试这个:
int resourceID = getResources().getIdentifier("list_" + i, "string", getPackageName());
String xxx = getString(resourceID);
所以我在strings.xml里面有很多字符串,它们的记录格式是:
<string name="list_1">xxxxxxxxxxx</string>
<string name="list_2">xxxxxxxxxxx</string>
<string name="list_3">xxxxxxxxxxx</string>
......
现在我想一个一个地加载它们,而不必键入所有的字符串 ID。我想以这样的方式加载它们:
for (int i = 1; i <= num; i++) {
// Just showing what I mean.
String xxx = getString(R.string.("list_" + i));
}
有方法吗?
值得使用资源数组。例如
<?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>
即可访问,迭代如下:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
试试这个:
int resourceID = getResources().getIdentifier("list_" + i, "string", getPackageName());
String xxx = getString(resourceID);