1

I have a ListView binded with custom adapter and its working fine :

private class Placeslist extends ArrayAdapter<ArrayList<String>> {

    private ArrayList<ArrayList<String>> items;

    public Placeslist(Context context, int textViewResourceId,
            ArrayList<ArrayList<String>> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.locationgpsrow, null);
        }
        ArrayList<String> o = items.get(position);
        if (o != null) {
                        // Binding here
                        TextView tv1 = (TextView) v.findViewById(R.id.txt1);
                        tv1.setText(o.get(0));
                        ImageView imView = (ImageView) v.findViewById(R.id.imageView1);
                        //o.get(1) // Image url e.g. http://www.some-website/image.jpg
                     }
        return v;
    }
}

I have an image source url in one of my array elements and i need to download the image and set it to ImageView in the listview item custom layout. And, I have code for that too !!

ImageView imView = (ImageView) findViewById(R.id.imageView1);
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();
            bmImg = BitmapFactory.decodeStream(is);
            is.close();

imView.setImageBitmap(Bitmap.createScaledBitmap(bmImg, width, height, true));

I can use it in my above getView() to set imageView source. But what i want is to let the list view bind to adapter without loading images(because it'll be heavy process). And then after listView is loaded then to initiate binding all the ImageViews in rows respectively. Is their a way to achieve this or any thing that can separate the image downloading process from listView binding ?

5 Answers 5

1

You have to use below lib to load image on run time :-

Universal loader lib

Picasso

Urlhelper

Sign up to request clarification or add additional context in comments.

Comments

1

You can go for 3rd party libraries like universalimageloader and others for it.

Otherwise, if you want to handle it on your own, just create an asyncTask to download an image from url and set it to the imageView onPostExecute callBack. You can pass the parameters(ImageView img, String Url) to this AsyncTask's Constructor, download the image in DoinBackground() and finally set it to the img onPostExecute();

You can trigger this asyncTask from the getView() method from the Adapter, may be doing this on UI thread may be required.

Comments

1

I have used Picasso Image library which takes care of caching and downloading easily. Have a look here: http://square.github.io/picasso/

Universal Image Loader also does this well, with a bit more options for configuration: https://github.com/nostra13/Android-Universal-Image-Loader

You might want to look at using a library like this as it will save data, and not do as much network operations.

1 Comment

Thanks !! I used Universal loader lib
0

Use SmartImageView to load the url to images. http://loopj.com/android-smart-image-view/

Comments

0

Your need to read about Lazy Loading of listview where the images are loaded asynchronously. You can use any of the libraries available out there.

A good complete tutorial about listview and lazy loading :

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.