我的xml中有这个数组
<string-array name = "locations">
<item value = "KDLH">Duluth International Airport</item>
<item value = "KBJI">Bemidji</item>
<item value = "KAUM">Austin Municipal</item>
<item value = "KBDE">Baudette International Airport</item>
<item value = "KBBB">Benson Municipal</item>
<item value = "KCBG">Cambridge Municipal</item>
<item value = "KCQM">Cook Municipal Airport </item>
<item value = "KCOQ">Cloquet</item>
<item value = "KTOB">Dodge Center Airport </item>
<item value = "KEVM">Eveleth</item>
<item value = "KLXL">Little Falls</item>
<item value = "KMML">Marshall</item>
<item value = "KANE">Minneapolis / Blaine </item>
<item value = "KLVN">Minneapolis, Airlake Airport</item>
<item value = "KMSP">Minneapolis-St. Paul</item>
</string-array>
我正在尝试在选择项目时检索值,而不是文本。我该怎么做呢?目前我只能通过这样做来检索文本。我目前只能检索所选文本而不是值属性。
答案 0 :(得分:4)
对此并不是一个很好的解决方案。这就是我通常的做法。
<string-array name="locations">
<item>Duluth International Airport</item>
<item>Bemidji</item>
<item>Austin Municipal</item>
<item>Baudette International Airport</item>
<item>Benson Municipal</item>
<item>Cambridge Municipal</item>
<item>Cook Municipal Airport </item>
<item>Cloquet</item>
<item>Dodge Center Airport </item>
<item>Eveleth</item>
<item>Little Falls</item>
<item>Marshall</item>
<item>Minneapolis / Blaine </item>
<item>Minneapolis, Airlake Airport</item>
<item>Minneapolis-St. Paul</item>
</string-array>
<string-array name="location_values">
<item>KDLH</item>
<item>KBJI</item>
<item>KAUM</item>
<item>KBDE</item>
<item>KBBB</item>
<item>KCBG</item>
<item>KCQM</item>
<item>KCOQ</item>
<item>KTOB</item>
<item>KEVM</item>
<item>KLXL</item>
<item>KMML</item>
<item>KANE</item>
<item>KLVN</item>
<item>KMSP</item>
</string-array>
然后你可以从location-values
数组中提取值。
答案 1 :(得分:1)
虽然我更喜欢McCracken的答案,但您可以将每一对都存储一些特殊的格式,以后可以解析。
示例:
<string-array name = "locations">
<item>KDLH:Duluth International Airport</item>
<item>KBJI:Bemidji</item>
...
</string-array>
然后:
String location = context.getResources().getStringArray(R.array.locations)[index];
String[] pieces = location.split(":");
String code = pieces[0]; // KDLH
String name = pieces[1]; // Duluth International Airport