Get attribute name from string array

你。 提交于 2019-12-01 03:21:50
Ravi Rupareliya

In the documentation there is no name attribute for an <item>.

So I don't think there will be any way to get those keys.

However, if you want to get name of string or string-array, you can get it programmatically but not for the <item>.

As "001" is just the index, why not simply use that?

<string-array name="USA">
    <item>NY</item>
    <item>LA</item>
</string-array>

Then just use index + 1 for the position:

String[] usaStates = getResources().getStringArray(R.array.USA);

int index = 0;

String firstStateName = usaStates[index];
int firstStatePosition = (index + 1);

That aside, you can use two arrays and merge them into a HashMap:

<string-array name="USA">
    <item>NY</item>
    <item>LA</item>
</string-array>

<string-array name="USA_pos">
    <item>001</item>
    <item>002</item>
</string-array>

String[] usaStates = getResources().getStringArray(R.array.USA);
String[] usaStatePositions = getResources().getStringArray(R.array.USA_pos);

Map <String, String> map = new HashMap<>(usaStates.length);

for (int i = 0; i < usaStates.length; i++) {
    map.put(usaStates[i], usaStatePositions[i]);
}
String[] numbers = getResources().getStringArray(R.array.USA);

to get data from array use.

numbers[id]

add array like this.

<string-array name="USA">
    <item>NY</item>
    <item>LA</item>
    <item>WA</item>
</string-array>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!