0

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 show : Video 1, Video 2
And on the click it launch the link (via an intent.putExtra).

How can I do to make this ?

Thank you very much

Joachim Sauer
  • 278,207
  • 54
  • 523
  • 586
Picool
  • 1

1 Answers1

0
  1. Parse your XML into a map. Map<VideoName,VideoLink>
  2. Set the list text to the keys of your map.
  3. 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;
}
Ujwal Parker
  • 682
  • 7
  • 11
  • Thank you very much for this help, list text to the key ? Do you have a tutorial for this ? Thanks – Picool Mar 09 '11 at 09:25
  • Extract the keys from your map and map them into an array.Go through the ListView tutorial [HERE](http://developer.android.com/resources/tutorials/views/hello-listview.html). Read setListAdapter. The adapter takes an array, this is where the array of keys is used. – Ujwal Parker Mar 10 '11 at 06:25
  • If I understand correctly : I should do : ArrayList> listItem = new 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 Mar 10 '11 at 10:34