如何将<string-array>转换为HashMap

时间:2015-11-19 05:40:42

标签: android arrays hashmap

这是天气应用程序代码的一部分。

我在strings.xml中编写了以下代码,但现在我想用java文件编写。我不知道如何在HashMap中编写以下代码并从中获取值。

以下是我的XML中的string-array

   <!-- Weather condtion 5,6 together with Dress -->
    <!-- Weather condtion 11,12 together with Dress -->
    <string-array name="eleven">
        <item name="dress_6">dress_6</item>
        <item name="dress_0">dress_0</item>
        <item name="dress_1">dress_1</item>
    </string-array>
    <!-- Weather condtion 9 together with Dress -->
    <string-array name="nine">
        <item name="dress_6">dress_6</item>
        <item name="dress_4">dress_4</item>
        <item name="dress_14">dress_14</item>
    </string-array>
    <!-- Weather condtion 5,6 together with Dress -->
    <string-array name="five">
        <item name="dress_2">dress_2</item>
        <item name="dress_8">dress_8</item>
        <item name="dress_6">dress_6</item>
    </string-array>
    <!-- condtion 2 -->
    <string-array name="two">
        <item name="dress_11">dress_11</item>
        <item name="dress_5">dress_5</item>
        <item name="dress_0">dress_0</item>
    </string-array>
    <string-array name="twentyFive">
        <item name="dress_0">dress_0</item>
        <item name="dress_3">dress_3</item>
        <item name="dress_8">dress_8</item>
    </string-array>
    <string-array name="twentySix">
        <item name="dress_12">dress_12</item>
        <item name="dress_13">dress_13</item>
        <item name="dress_5">dress_5</item>
    </string-array>
    <string-array name="thirtySix">
        <item name="dress_11">dress_11</item>
        <item name="dress_9">dress_9</item>
        <item name="dress_4">dress_4</item>
    </string-array>
    <string-array name="thirtySeven">
        <item name="dress_12">dress_12</item>
        <item name="dress_13">dress_13</item>
        <item name="dress_3">dress_3</item>
    </string-array>

2 个答案:

答案 0 :(得分:0)

让我们试试这个:

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;

public class Test {

public static void main(String[] args) {

    Map<String,String> map = new HashMap<String,String>();
    map.put("name","chris");
    map.put("island","faranga");

    XStream magicApi = new XStream();
    magicApi.registerConverter(new MapEntryConverter());
    magicApi.alias("root", Map.class);

    String xml = magicApi.toXML(map);
    System.out.println("Result of tweaked XStream toXml()");
    System.out.println(xml);

    Map<String, String> extractedMap = (Map<String, String>) magicApi.fromXML(xml);
    assert extractedMap.get("name").equals("chris");
    assert extractedMap.get("island").equals("faranga");

}

public static class MapEntryConverter implements Converter {

    public boolean canConvert(Class clazz) {
        return AbstractMap.class.isAssignableFrom(clazz);
    }

    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {

        AbstractMap map = (AbstractMap) value;
        for (Object obj : map.entrySet()) {
            Map.Entry entry = (Map.Entry) obj;
            writer.startNode(entry.getKey().toString());
            Object val = entry.getValue();
            if ( null != val ) {
                writer.setValue(val.toString());
            }
            writer.endNode();
        }

    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

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

        while(reader.hasMoreChildren()) {
            reader.moveDown();

            String key = reader.getNodeName(); // nodeName aka element's name
            String value = reader.getValue();
            map.put(key, value);

            reader.moveUp();
        }

        return map;
    }

}

}

请查看THIS以获取更多信息。

答案 1 :(得分:0)

我认为您正在寻找填充HashMap的方法

您可以使用以下

Map<String, List<String>> map = new HashMap<>();
map.put("eleven", Arrays.asList("dress_6", "dress_0", "dress_1"));

如果您需要更复杂的结构,可以使用

之类的东西
Map<String, Map<String, String>> map = new HashMap<>();

要从地图中检索,您可以使用类似

的内容
List<String> list = map.get("eleven");

此列表将包含您添加的元素&#34; dress_6&#34;,&#34; dress_0&#34;,&#34; dress_1&#34;

您可以在for循环中以

的形式访问这些元素
for(String item : list) {
    System.out.println("Item: " + item);
}

您尚未指定确切的用例,但我建议不要对配置进行硬编码。您可以从现有的string.xml文件中读取并在启动或首次访问时填充Map。