[英]Get value along with String in string.xml file
I have the following in my strings.xml file: 我的strings.xml文件中包含以下内容:
<resources>
<string name="app_name">My Application</string>
<string name="title_activity_maps">Map</string>
<string-array name="city_names">
<item value="1452">Abbotsford</item>
<item value="163">Aberdeen</item>
<item value="821">Acornhoek</item>
<item value="168">Addo</item>
<item value="153">Adelaide</item>
<item value="1433">Aeroton</item>
<item value="790">Agatha</item>
<item value="393">Aggeneys</item>
<item value="867">Agulhas</item>
</string-array>
I'm using it for an AutoCompleteTextView. 我将其用于AutoCompleteTextView。 My question is, how can I get the value to be printed to the logs after the user searches a city? 我的问题是,用户搜索城市后,如何获取要打印到日志中的值? I'm using this to get the city names, but also wants the value. 我正在使用它来获取城市名称,但也需要该值。
cityNames = getResources().getStringArray(R.array.city_names);
This can't be done this way and there is no value
attribute in <item>
XML tag. 这无法通过这种方式完成,并且<item>
XML标记中没有value
属性。 What you can do is that you have your string as a pattern and try to parse that pattern in two different values using string manipulation or JSON or any structured data format. 您可以做的是将字符串作为模式,并尝试使用字符串操作或JSON或任何结构化数据格式将模式解析为两个不同的值。
Example: 例:
<string-array name="city_names">
<item>Abbotsford,1452</item>
<item>Aberdeen,163</item>
</string-array>
and then parse that using the split
on the string to get your two values. 然后使用字符串上的split
解析它,以获取两个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.