- 分析XML到地圖中。
Map<VideoName,VideoLink>
- 將列表文本設置爲您的地圖的鍵。
- 爲您的列表中的onClick監聽器中,你應該得到被點擊的按鍵和檢索值並啓動意圖。
這裏是你如何做到這一點:
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;
}
非常感謝您的幫助,列出關鍵文本?你有這個教程嗎?謝謝 – Picool 2011-03-09 09:25:49
從地圖中提取鍵並將它們映射到數組中。請瀏覽ListView教程[HERE](http://developer.android.com/resources/tutorials/views/hello-listview.html)。閱讀setListAdapter。適配器需要一個數組,這是使用鍵數組的地方。 – 2011-03-10 06:25:04
如果正確地明白: 我應該做的: 的ArrayList>的listItem =新的ArrayList >(); HashMap map; map = new HashMap (); map.put(「titre」,PARSING_BUT_DONT_KNOW_HOW); map.put(「url」,PARSING_BUT_DONT_KNOW_HOW); listItem.add(map); String [] videoname = getResources()。getStringArray(R.array.videoname_array); setListAdapter(new ArrayAdapter (this,R.layout.list_item,videoname)); –
Picool
2011-03-10 10:34:46