字符串列表&LT;数组列表中的LT和增加值的字符串&GT;&GT;在HashMap中的Android [英] add the value in Arraylist<string,List<String>> in hashmap android
问题描述
我要获取数据的LIS。
I have to fetch the lis of data.
所以我必须使用字符串list.here的ArrayList中如何我添加在地图上的价值。
So i have used the arraylist of string with list.here how can i add the value on map.
我用低于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);
}
下面我得到这些提示以下错误:(map.put(KEY_TITLE,((元)nl.item(I))的getAttribute(名字));。)
行:
Here am getting the below error on these (map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));)
line:
The method put(String, List<String>) in the type HashMap<String,List<String>> is not applicable for the arguments (String, String)
我怎样才能清除这些错误..pls我提供的解决方案来...
How can i clear these error ..pls provide me solution for these ...
推荐答案
在此行
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.
例如:
List<String> myList= new ArrayList<String>();
myList.add((Element)nl.item(i)).getAttribute("name"));
map.put( KEY_TITLE,myList);
您有一个循环。我认为,我知道你想实现我会更新我的code什么。这可以帮助您避免覆盖地图输入
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);
}
方法populateMap()
Method 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);
}
}
这篇关于字符串列表&LT;数组列表中的LT和增加值的字符串&GT;&GT;在HashMap中的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!