我想使用parsing XML (文件在网上)来获取listView中的标题,然后在单击时使用我解析的一部分调用活动。
示例:我的xml内容:
<title>Video 1</title><link>http://video.mp4</link>
<title>Video 2</title><link>http://video2.mp4</link>ListView节目:视频1、视频2
然后单击它启动链接(通过intent.putExtra)。
我该怎么做才能做到这一点?
非常感谢
发布于 2011-03-09 17:13:53
Map<VideoName,VideoLink>下面是你如何做到这一点:
final HashMap<String, String> xmlMap = xmlToMap();
final String[] titleArray=(String[]) xmlMap.keySet().toArray();
ListView lv=new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 ,titleArray));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
{
String videoTitle=titleArray[pos];
String videoLink=xmlMap.get(titleArray[pos]);
}
});
HashMap<String, String> xmlToMap()
{
HashMap<String, String> xmlMap = new HashMap<String, String>();
// Parse the xml document you have.
// In the xml parsing loop
// Every 'title' tag you read and corresponding 'link'
//tag you read you insert an element into map
String title=""; // Every 'title' tag you read
String link=""; // Corresponding 'link' tag
xmlMap.put(title, link);
// Loop ends
return xmlMap;
}https://stackoverflow.com/questions/5243453
复制相似问题