Parsing XML = variable for Intent
I would like use parsing XML (the file is on the Net) to get a title in a listView and on the click call an Activity with a part of my parsing.
Example : My xml contents :
<title>Video 1</title><link>http://video.mp4</link>
<title>Video 2</title><link>http://video2.mp4</link>
ListView sh开发者_如何学运维ow : Video 1, Video 2
And on the click it launch the link (via anintent.putExtra
).
How can I do to make this ?
Thank you very much
- Parse your XML into a map.
Map<VideoName,VideoLink>
- Set the list text to the keys of your map.
- Create a onClick listener for your list in which you should get the key that was clicked and retrieve the value and start the intent.
Here is how you do it:
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;
}
精彩评论