2
votes

For my custom list view, I'm using AsyncTask for the web service call and it is working fine. My List view have a left side image, title, descriptions. the service will return a list of (url, title, desc).

And in my adapter, i'm loading the images using the following code:

httpClient = new DefaultHttpClient(); 
HttpGet request = new HttpGet(url);         
response = httpClient.execute(request);
InputStream is = response.getEntity().getContent();
Drawable drawable = Drawable.createFromStream(is, "src");
imgView.setImageDrawable(drawable);

But I'm not able to see the list view until all the images loaded, I know i'm blocking the UI thread, can anybody suggest the best way to do this without UI blocking.

Thanks, Venkat Papana

4
The concept you need to implement is known as "ListView - Image Lazy Loading" and here is the best example i have ever found: stackoverflow.com/questions/541966/… , just check this.Paresh Mayani

4 Answers

3
votes

Use an AsyncTask or a Thread with a Handler like the above answers have noted.

Here is a quick way to get an image resource from a URL:

BitmapDrawable bitmapDrawable = new BitmapDrawable(BitmapFactory.decodeStream(new URL("http://example.com/path/to/image/file.jpg").openStream()));

Create 1 AsyncTask, and in the doInBackground, loop through all your image URLS and add each resulting drawable to an icon list, or something

2
votes
new Thread(new Runnable() {

                           @Override
                           public void run() {

                                       httpClient = new DefaultHttpClient(); 
                                       HttpGet request = new HttpGet(url);         
                                       response = httpClient.execute(request);
                                       InputStream is =  response.getEntity().getContent();
                                     msg.obj=is;
                                   mHandler.sendMessage(msg);
                           }
                   }).start();

and your handle will be like this

mHandler = new Handler() { 
              @Override public void handleMessage(Message msg) { 
                 InputStream is=(InputStream)msg.obj;
                 Drawable drawable = Drawable.createFromStream(is, "src");
                 imgView.setImageDrawable(drawable);

              }
          };
1
votes

Use an AsyncTask for image loading as well. For each loaded image, you can call publishResults() from the doInBackground() and then have onProgressUpdate() put the image in the right place.

1
votes

This is the better way just try.. this is for image load in cardview

private class LoadImage extends AsyncTask<String, String, Bitmap> {
    Bitmap bitmap1 = null;
    CurrentVehicleStatus currentVehicleStatus;

    int i;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Stuff on preexecute

        pDialog = new ProgressDialog(AdminActivity.this);
        pDialog.setTitle("Please wait");
        pDialog.setMessage("Loading data..");
        pDialog.show();

    }
    protected Bitmap doInBackground(String... args) {

           for(int i=0;i<arrayLength;i++) {
          //Your class reference that store your values for adapter
          yourClassReference = yourArrayList.get(i);

            try {
                bitmap = BitmapFactory.decodeStream((InputStream) new URL("Your URL"
                ).getContent());

            } catch (Exception e) {
                e.printStackTrace();
            }
    //this setImage method in data class
      yourArrayList.get(i).setImage(bitmap);

         }

        }


        return bitmap1;
    }

    protected void onPostExecute(Bitmap image) {

        pDialog.dismiss();
    //Notify adapter
        mAdapter.notifyDataSetChanged();

    }
}

is there any doubt comment me