Search code examples
javaandroidjsoup

Get more than one Element JSoup Java Android


I am trying to get a list of items to form a playlist, and I am only able to retrieve one of the items. Here is the code I have going to my recyclerview's bindView:

@Override
public void onBindViewHolder(PlaylistViewHolder holder, int position)
{

    try
    {
        String url = "https://www.c895.org/playlist";
        Document document = Jsoup.connect(url).get();

        Element playlist = document.select("#playlist").first();

        List<TrackInfo> tracks = new ArrayList<>();

        for(Element track : playlist.children())
        {
            long time = Long.parseLong(track.dataset().get("ts"));
            String title = track.select(".title").text();
            String artist = track.select(".artist").text();

            tracks.add(new TrackInfo(new Date(time * 1000), title, artist));
        }
        for(int i = 0; i < tracks.size() - 1; i++)
        {

            holder.titlesView.setText(tracks.get(i).toString());
        }
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

}

Ideally I'd like to get about 10-20 results. Is there anyway I could do this?


Solution

  • It's because the html part that you need is in the following tag:

    <div id="playlist">
    
    </div>
    

    So you can't use the following:

    Element playlist = document.select("#playlist").first();
    

    but you need to use div#playlist to get all the playlist item:

    Element playlist = document.select("div#playlist");