[英]How do I obtain the values of LinkedList<HashMap<String,String>>?
I have this XMLParser: 我有这个XMLParser:
public class XMLParser {
private URL url;
public XMLParser(String url){
try {
this.url = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public LinkedList<HashMap<String, String>> parse() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
LinkedList<HashMap<String, String>> entries = new LinkedList<HashMap<String,String>>();
HashMap<String, String> entry;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.url.openConnection().getInputStream());
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName("item");
for (int i=0; i<items.getLength(); i++) {
entry = new HashMap<String, String>();
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j=0; j<properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("title")) {
entry.put(CheckNewPost.DATA_TITLE, property.getFirstChild().getNodeValue());
} else if (name.equalsIgnoreCase("link")) {
entry.put(CheckNewPost.DATA_LINK, property.getFirstChild().getNodeValue());
}
}
entries.add(entry);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return entries;
}
}
The problem is that I don't know how to obtain last value added on LinkedList and show it on a Toast. 问题是我不知道如何获取LinkedList上添加的最后一个值并将其显示在Toast上。 I tried this:
我尝试了这个:
XMLParser par = new XMLParser(feedUrl);
HashMap<String, String> entry = par.parse().getLast();
String[] text = new String[] {entry.get(DATA_TITLE), entry.get(DATA_LINK)};
Toast t = Toast.makeText(getApplicationContext(), text[0], Toast.LENGTH_LONG);
t.show();
Evidently I don't have sufficient experience and I know almost nothing about Map, HashMap or so… The XMLParser is not mine. 显然,我没有足够的经验,并且我对Map,HashMap等等一无所知。XMLParser不是我的。
For iterating through all the values in your HashMap, you can use an EntrySet: 要遍历HashMap中的所有值,可以使用EntrySet:
for (Map.Entry<String, String> entry : hashMap.entrySet()) {
Log.i(TAG, "Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
And for only obtaining the last value, you can try this: 并且仅获取最后一个值,您可以尝试以下操作:
hashMap.get(hashMap.size() - 1);
So the entry is HashMap that mean you need to get the Named/Value pairs out of the Map. 因此,该条目是HashMap,这意味着您需要从Map中获取“名称/值”对。 So for map, there is Named also known as Key or (hashed index), and for each "Key" there exist exactly one "value" so that.
因此,对于map来说,有一个Named也称为Key或(哈希索引),并且对于每个“ Key”都存在一个“值”,因此。
Map<String, String> entries = new HashMap<String, String>();
entries.put("Key1", "Value for Key 1");
entries.put("Key 2", "Key 2 value!");
entries.put("Key1", "Not the original value for sure!"); // replaced
and later you can get it as: 然后您可以将其获取为:
String val1 = entries.get("Key1"); // "Not the original value for sure!"
String val2 = entries.get("Key 2"); //"Key 2 value!"
and to loop through the Map you would do: 并遍历地图,您将执行以下操作:
for (String key : entries.keySet()) {
String value = entries.get(key);
}
Hope that help. 希望对您有所帮助。 You can also google "Java Map example" for more.
您还可以通过Google搜索“ Java Map示例”了解更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.