2011-03-09 50 views
0

我想使用解析XML(該文件是在網絡上)在ListView獲得冠軍,並在點擊呼叫的活動與我的分析的一部分。解析XML =變量意向

例子: 我的XML內容:

<title>Video 1</title><link>http://video.mp4</link> 
<title>Video 2</title><link>http://video2.mp4</link> 

的ListView顯示:視頻1視頻2
而就在點擊啓動鏈接(通過intent.putExtra)。

我該怎麼做才能使這個?

非常感謝您

回答

0
  1. 分析XML到地圖中。 Map<VideoName,VideoLink>
  2. 將列表文本設置爲您的地圖的鍵。
  3. 爲您的列表中的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; 
} 
+0

非常感謝您的幫助,列出關鍵文本?你有這個教程嗎?謝謝 – Picool 2011-03-09 09:25:49

+0

從地圖中提取鍵並將它們映射到數組中。請瀏覽ListView教程[HERE](http://developer.android.com/resources/tutorials/views/hello-listview.html)。閱讀setListAdapter。適配器需要一個數組,這是使用鍵數組的地方。 – 2011-03-10 06:25:04

+0

如果正確地明白: 我應該做的: 的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

0

自帶了Android的文檔有一些教程和信息,可以對您有用。

有關創建列表視圖的說明,請參閱本link

從代碼中具有一定的URL啓動一個網頁瀏覽器的活動,看到這個topic

+0

嗨,創建listView它對我來說是好的^^ 但我不明白爲什麼webbrowser活動?我已經有播放視頻的代碼,問題是用xml解析^^。 謝謝 – Picool 2011-03-09 09:26:58

+0

你是否在解析本身有問題,或者你在尋找一種使用解析數據的好方法? – Ithildin 2011-03-09 09:39:19

+0

嗨2 ^^,我開始使用XML解析,所以我沒有找到很好的教程來幫助我 – Picool 2011-03-09 10:05:02