java 将 Arraylist<string,List<String>> 中的值添加到 hashmap android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15738659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:42:11  来源:igfitidea点击:

add the value in Arraylist<string,List<String>> in hashmap android

javaandroidxml-parsingarraylisthashmap

提问by user2218667

I have to fetch the lis of data.

我必须获取数据列表。

So i have used the arraylist of string with list.here how can i add the value on map.

所以我使用了带有 list.here 的字符串数组列表,我如何在地图上添加值。

I have used below code:

我使用了以下代码:

       static final String KEY_TITLE = "Category";
         static final String KEY_ARTICLE = "article";
          static final String KEY_IMAGE = "image";


 final ArrayList<HashMap<String,  List<String>>> songsList = new ArrayList<HashMap<String,  List<String>>>();
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_TITLE);
        NodeList nl1 = doc.getElementsByTagName(KEY_ARTICLE);
        NodeList nl2 = doc.getElementsByTagName(KEY_IMAGE);
        System.out.println(nl.getLength());
        for (int i = 0; i < nl.getLength(); i++) {
           //System.out.println(nl.getLength());
            HashMap<String, List<String>> map = new HashMap<String, List<String>>();
            map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));
           for (int j = 0; j < nl1.getLength(); j++) {
              map.put( KEY_ARTICLE,((Element)nl1.item(j)).getAttribute("title"));
              map.put( KEY_IMAGE,((Element)nl2.item(j)).getAttribute("url"));
             }
           songsList.add(map);
          }

Here am getting the below error on these (map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));)line:

这是在这些(map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));)行上收到以下错误:

The method put(String, List<String>) in the type HashMap<String,List<String>> is not applicable for the arguments (String, String)

How can i clear these error ..pls provide me solution for these ...

我怎样才能清除这些错误..请为我提供解决方案......

回答by Festus Tamakloe

at this line

在这一行

 map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));

you are adding a single string to instead of List

您正在添加单个字符串而不是列表

You have to create a List of String at that point and add your String to it and at the list to your map.

您必须在那时创建一个字符串列表并将您的字符串添加到它并在列表中添加到您的地图。

e.g.:

例如:

  List<String> myList= new ArrayList<String>();
  myList.add((Element)nl.item(i)).getAttribute("name"));
  map.put( KEY_TITLE,myList);

you have a loop. I think that i know what you want to achieve i'll update my code. This can help you avoiding overriding the map input

你有一个循环。我想我知道你想要实现我会更新我的代码。这可以帮助您避免覆盖地图输入

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

        for(int i = 0; i < nl.getLength(); i++) {
             String name = (Element) nl.item(i)).getAttribute("name");
             populateMap(map, name, KEY_TITLE);
            for(int j = 0; j < nl1.getLength(); j++) {
                String title=(Element) nl1.item(j)).getAttribute("title");
                populateMap(map, title, KEY_ARTICLE);

                String url=(Element) nl2.item(j)).getAttribute("url");
                populateMap(map, url, KEY_IMAGE);
            }
            songsList.add(map);
        }

Method populateMap()

方法 populateMap()

void populateMap(HashMap<String, List<String>> map, String value, String key) {
        List<String> myList;
        if(!map.containsKey(key)) {
            myList = new ArrayList<String>();
            myList.add(value);
            map.put(key, myList);
        } else {
            myList = map.get(key);
            myList.add(value);
        }
    }

回答by Juvanis

You get that error because a Stringis not a List<String>.

你得到那个错误是因为 aString不是 aList<String>.

Either change your map declaration to HashMap<String, String>or enclose your Stringinto a List<String>and give it to the method.

将您的地图声明更改为HashMap<String, String>或将您的声明包含在Stringa 中List<String>并将其提供给该方法。

回答by Gabe Sechan

The key you're passing in is wrong. You're passing in a string, you need to pass in a list of strings. So create a list, stick the value you're trying to pass into the hash map into the list, then put that list into the hash map.

您传入的密钥是错误的。你传入一个字符串,你需要传入一个字符串列表。因此,创建一个列表,将您尝试传递到哈希映射中的值粘贴到列表中,然后将该列表放入哈希映射中。