Question

I can't get my head around this, I can manage to get which row number of the listview is clicked. But I can't figure out how to put the text of the selected list row into a variable. I need this just to move onto content that is specific to the selected list row. Any help or direction would be great,thanks!

ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }


        list=(ListView)rootView.findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(getActivity(), songsList); ;        
        list.setAdapter(adapter);


        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
            { 
                String TEST = arg0.getItemAtPosition(position).toString();


                Toast.makeText(getActivity(), TEST, Toast.LENGTH_SHORT).show();
            }
                });




        return rootView;        
    }   
}
Was it helpful?

Solution

The third argument in onItemClick() is position.
Use this position to .getItem() from the ListAdapter associated with your ListView. You can then get the text from that item in the normal way.

<Additional>

To get the ListAdapter associated with your ListView you would use arg0.getAdapter(). So the full method call becomes arg0.getAdapter().getItem(position).

but as ρяσѕρєя K pointed out, you defined your ListAdapter using the ArrayList called songList.
You defined songList is an ArrayList of HashMaps.
So when you call arg0.getAdapter().getItem(position), what will be returned is the HashMap located at that position in your ArrayList called songList.
Since a HashMap is what will be returned, you should write something like:

HashMap selectedItemHashMap = (HashMap)arg0.getAdapter().getItem(position);

You can then retrieve the relevant data from the HashMap with:

selectedItemHashMap.get(...//The relevant key for the data you want to get out of the HashMap...)

OTHER TIPS

Because you are passing ArrayList of HaspMap to LazyAdapter so getItemAtPosition will return you HaspMap at the position of selected row instead of String. set select row elements as:

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
       HashMap<String, String> selected_row =(HashMap<String, String>) 
                       list.getItemAtPosition(position);

          String str_title=selected_row.get(KEY_TITLE);
          String str_artist=selected_row.get(KEY_ARTIST);
        //....
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top