getResourceEntryName一个数组项数组、getResourceEntryName

2023-09-04 03:38:00 作者:哥儿几个走着*

我有一个字符串数组中的strings.xml详情如下:

I have a string array in strings.xml as detailed below:

<string-array name="spinerArray">
    <item name="one">First</item>
    <item name="two">Second</item>
    <item name="three">Third</item>
    <item name="four">Fourth</item>
</string-array>

和我需要检索的项目一个名称不是值(一,二,三,四)。到目前为止,我知道这是可能获得使用getResourceEntryName像数组的名称:

And I need to retrieve one of the items name not the value (one, two, three, four). So far, I know it's possible to get the name of the array using getResourceEntryName like:

getResources().getResourceEntryName(R.array.spinerArray);

有没有办法让一个项目的名字吗?

Is there a way to get an item name?

感谢。

推荐答案

另一种解决方案:

Resources res = getResources();
TypedArray entriesTypedArray = res.obtainTypedArray(R.array.spinerArray);
ArrayList entriesName = new ArrayList();
TypedValue v1 = new TypedValue();
for (int i1=0; i1<entriesTypedArray.length(); i1++) {
    entriesTypedArray.getValue(i1, v1);
    String name1 = res.getResourceEntryName(v1.resourceId);
    entriesName.add(name1);
}