S
S
Sebastian Pereira2016-11-13 20:14:26
Android
Sebastian Pereira, 2016-11-13 20:14:26

Display images in ListView?

To download an image I use:
downloadImage

public static Bitmap downloadImage(String iUrl) {
    Bitmap bitmap = null;
    HttpURLConnection conn = null;
    BufferedInputStream buf_stream = null;
    try {
        Log.v(TAG, "Starting loading image by URL: " + iUrl);
        conn = (HttpURLConnection) new URL(iUrl).openConnection();
        conn.setDoInput(true);
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.connect();
        buf_stream = new BufferedInputStream(conn.getInputStream(), 8192);
        bitmap = BitmapFactory.decodeStream(buf_stream);
        buf_stream.close();
        conn.disconnect();
        buf_stream = null;
        conn = null;
    } catch (MalformedURLException ex) {
        Log.e(TAG, "Url parsing was failed: " + iUrl);
    } catch (IOException ex) {
        Log.d(TAG, iUrl + " does not exists");
    } catch (OutOfMemoryError e) {
        Log.w(TAG, "Out of memory!!!");
        return null;
    } finally {
        if ( buf_stream != null )
            try { buf_stream.close(); } catch (IOException ex) {}
        if ( conn != null )
            conn.disconnect();
    }
    return bitmap;
}

To update the ListView
try{
                        JSONObject jsonResponse = new JSONObject(response.toString());
                        JSONArray jsonMainNode = jsonResponse.getJSONArray("items");
                        //JSONArray Data = jsonResponse.getJSONArray("snippet");

                        for(int i = 0; i<jsonMainNode.length();i++){

                            JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                            String name = jsonChildNode.optString("kind");
                            String number = jsonChildNode.optString("etag");


                            JSONObject item = jsonMainNode.getJSONObject(i);
                            JSONObject snippet = item.getJSONObject("snippet");

                            String title = snippet.getString("title");
                            String channelTitle = snippet.getString("channelTitle");
                            String pubDate = snippet.getString("publishedAt");

                            JSONObject thumbs = snippet.getJSONObject("thumbnails");
                            JSONObject thumb = thumbs.getJSONObject("default");
                            final String ico = thumb.getString("url");



                            new Thread(new Runnable() {
                                public void run() {

                                    bmp = ImageManager.downloadImage(ico);

                                }
                            }).start();






                            countryList.add(createEmployee(title,channelTitle,pubDate, bmp));
                        }

simpleAdapter.notifyDataSetChanged();

The Listview is updating, but the pictures are not loading. ListView is refreshed by button

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Gamega, 2016-11-13
@gadfi

picasso and don't reinvent the wheel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question