我有這樣的XMLParser的:如何獲取LinkedList的值<HashMap <String,String >>?
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;
}
}
的問題是,我不知道如何獲得最後的附加值上鍊表,並顯示在一個吐司。我嘗試這樣做:
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();
顯然,我沒有足夠的經驗,我幾乎一無所知地圖,HashMap的左右...... XMLParser的的是不是我的。
那個失敗了? – njzk2