简体   繁体   English

将值添加到Arraylist中 <string,List<String> &gt;在hashmap android中

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

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。一起使用了,在这里我如何在地图上添加值。

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 ... 我如何清除这些错误..pls为我提供了这些解决方案...

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. 此时,您必须创建一个字符串列表,然后将字符串添加到该列表中,并将该列表添加到地图中。

eg: 例如:

  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);
        }
    }

You get that error because a String is not a List<String>. 因为String不是List<String>. ,您会收到该错误List<String>.

Either change your map declaration to HashMap<String, String> or enclose your String into a List<String> and give it to the method. 将地图声明更改为HashMap<String, String>或将您的String括入List<String>并将其提供给方法。

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. 因此,创建一个列表,将要传递给哈希映射的值粘贴到列表中,然后将该列表放入哈希映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:获取整个数组,值/键在ArrayList中 <HashMap<String, String> &gt; - Android: Get the entire array the value/key is in ArrayList<HashMap<String, String>> Java/Android - 搜索 ArrayList <hashmap<string, string> &gt; 用于匹配键 -&gt; 值</hashmap<string,> - Java/Android - Search ArrayList<HashMap<String, String>> for matching key -> value 从ArrayList获得奇数或偶数值 <HashMap<String, String> &gt;在android中 - getting odd or even value from ArrayList<HashMap<String, String>> in android 数组列表 <HashMap<String, String> &gt;到SQLite数据库android - ArrayList<HashMap<String, String>> to SQLite database android Android:如何订购ArrayList <HashMap<String,String> &gt;? - Android: How to Order ArrayList<HashMap<String,String>>? 排序ArrayList <HashMap<String, String> &gt;使用价值 - sort ArrayList<HashMap<String, String>> using value 从HashMap获得价值 <String, ArrayList<String> &gt; - Get value from HashMap<String, ArrayList<String>> HashMap中 <String, ArrayList<String> &gt; - HashMap<String, ArrayList<String>> 在HashMap中的StringList中添加动态内容 - Add dynamic content in ArrayList of String in a HashMap 如何从ArrayList添加值 <HashMap<String, String> &gt; menuItems = new ArrayList <HashMap<String, String> &gt;编辑文本 - How to add values from ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>> to Edit Text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM