0

我的 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>

我试图在选择项目而不是文本时检索值。我该怎么做呢?目前我只能通过这样做来检索文本。我目前只能检索选定的文本,而不是 value 属性。

4

2 回答 2

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数组中提取值。

于 2013-04-18T05:04:53.673 回答
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
于 2013-04-18T05:24:25.293 回答