Search code examples
androidimagelistviewjsoupsimpleadapter

How to show images and text with listview from web site


I use Jsoup to parse HTML page and I want to show text and images in the ListView. So I created LinkedHashMap and SimpleAdapter for this. Besides text shows as is. Images don't wan't to be shown. Every time I get log message such as: "resolve Uri failed on bad bitmap uri". I tried to google this problem but still can't resolve it.

Here is the code:

 @Override
protected String doInBackground(String... arg) {


    Document doc;
    try {

        doc = Jsoup.connect("http://thesiteiuse.com/news/").get();

        title = doc.select("h2[class=et_pt_title]");
        picture = doc.select("img");

        listViewContent.clear();

        for (Element titles : title) {


            Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put(ATTRIBUTE_NAME_TEXT, titles.text());

            listViewContent.add(map);
        }
        for (Element img : picture){

            Bitmap bitmap;
            Map<String,Object> picMap = new LinkedHashMap<String,Object>();
            String iurl;
            iurl = img.absUrl("src");
            Log.w("ABSurl:", iurl.toString());
            URL url = new URL(iurl);
            bitmap = BitmapFactory.decodeStream(url.openStream());
            Log.w("BITMAP",bitmap.toString());


            picMap.put(ATTRIBUTE_NAME_IMAGE, String.valueOf(bitmap));
            listViewContent.add(picMap);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

How can I solve my problem? Maybe is there another simple way to display images in the ListView which app get from the URL?


Solution

  • You have to create your own adapter to the listview. You should see this tutorial on how to create it: http://www.vogella.com/tutorials/AndroidListView/article.html