我可以给字符串数组中的项目一个ID吗? [英] Can I give an ID to an item in String-array?
问题描述
我的问题很简单,但我不知道答案.
My question is simple, yet I don't know the answer.
我想要的内容:
<string-array name="items_array">
<item id="id1">Item1</item>
</string-array>
这是我目前拥有的:
<string-array name="items_array">
<item>Item1</item>
</string-array>
我想实现的目标是,在我的 Spinner
普通名称(例如例如Martin )中提供.但是,在该项目的 ID
中,我想输入"martin93" .
What do I want to achieve with that is, that I want to offer in my Spinner
normal names (e.g. Martin). Yet in the ID
of that item, I want to have e.g. "martin93".
ID
很有用,可将其附加到我的 URL ,我要从中获取一些数据.但是我希望用户选择一个普通"名称,而不是奇怪的(例如FaceBook)URL名称.
The ID
is usefull to append it to my URL, from which I want to fetch some data. But I want the user to choice a "normal" name, instead of weird (for example FaceBook) url name.
重要提示:只是出于解释目的给出了Facebook示例,我试图实现其他目标,但非常相似(使用ID我想获取真实URL的名称,这对阅读(如果我将其直接放入Spinner中).
IMPORTANT: The facebook example is just given for explanation purposes, I am trying to achieve something else, but very similar (with ID I want to fetch the real URL's name, that is not nice to read, if I'd put it directly into Spinner).
有什么想法吗?
推荐答案
通常您会拥有
<?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>
<string-array name="planets_array_values" translatable="false">
<item>merc</item>
<item>ven</item>
<item>eart</item>
<item>mars</item>
</string-array>
</resources>
然后用 planets_array
和
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String value = getResources().getStringArray(R.array.planets_array_values)[pos];
}
这篇关于我可以给字符串数组中的项目一个ID吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!