Jyosna
Jyosna

Reputation: 4446

android issue with populating the list item in a ListView

In my application, to populating a ListView i am using custom adapter, because one listitem consists of 3 TextViews and 1 ImageView. Everytime images are fetched from url.

So when i am launching this activity its taking much time,because its downloading all images then populating the lists.

So i want without images list should populate first having only Textviews and after that only images should come.

How can i do that?

Upvotes: 0

Views: 259

Answers (4)

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

By loading your images with an AsyncTask

example directly from documentation:

public void onClick(View v) {
    new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    /** The system calls this to perform work in a worker thread and
      * delivers it the parameters given to AsyncTask.execute() */
    protected Bitmap doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);
    }

    /** The system calls this to perform work in the UI thread and delivers
      * the result from doInBackground() */
    protected void onPostExecute(Bitmap result) {
        mImageView.setImageBitmap(result);
    }
}

Upvotes: 2

Sudarshan Bhat
Sudarshan Bhat

Reputation: 3772

you have to create a lazy image loader using async task.

by doing that, all your list view will be populated. and when the images are fetched, they are updated into the list views asynchronously.

Here is a link - http://iamvijayakumar.blogspot.com/2011/06/android-lazy-image-loader-example.html

Upvotes: 0

Gaurav Shah
Gaurav Shah

Reputation: 5279

The basic Idea is to have a loading image already within your app. Then use an asyncTask or a thread to load image.

Some code to start with :

Adapter

public class ImageAdapter extends BaseAdapter {
private static final String TAG = "Image Adapter";
int mGalleryItemBackground;
private Context mContext;
private GridView mView;


/** URL-Strings to some remote images. */
private String[] mRemoteImagesURL ;
private Bitmap[] loadedImages;


public ImageAdapter(Context c,String[] remoteImagesURL,GridView v) {
    mContext = c;
    TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
    mGalleryItemBackground = attr.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);
    attr.recycle();
    mView = v;
    mRemoteImagesURL=remoteImagesURL;
    loadedImages = new Bitmap[mRemoteImagesURL.length];

}


public int getCount() {
    return mRemoteImagesURL.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.gallery_item, null);
    }


    ImageView imageView = (ImageView) convertView.findViewById(R.id.FrontImageView);

    /* when image is already down-loaded then load that image */
    if(loadedImages[position]!=null)
        imageView.setImageBitmap(loadedImages[position]);
    else
        imageView.setImageResource(R.drawable.loading);

    imageView.setBackgroundResource(mGalleryItemBackground);


    return convertView;
}

public void loadImage(int position){
    Bitmap bm;

      try {
            /* Open a new URL and get the InputStream to load data from it. */
            URL aURL = new URL(mRemoteImagesURL[position]);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            /* Buffered is always good for a performance plus. */
            BufferedInputStream bis = new BufferedInputStream(is);
            /* Decode url-data to a bitmap. */
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
            loadedImages[position] =bm;

    } catch (Exception e) {

            Log.e(TAG, "Remote Image Load Exception"+e);
    }

}

public void setLoadedImage(int position)
{
    Log.d(TAG, "Position "+position);
    View childView= mView.getChildAt(position);
    if(loadedImages[position]!=null && childView != null)
    {
        ImageView imageView= (ImageView) childView.findViewById(R.id.FrontImageView);
        imageView.setImageBitmap(loadedImages[position]);           
    }
}

} private void updateImagesAsThread() { Thread t = new Thread() {

            public void run()
            {
                try {


               for(int i=0;i<imageAdapter.getCount();i++)
                {
                imageAdapter.loadImage(i);
                listAdapterHandler.sendEmptyMessage(i);
                }
                }
                catch (Exception e) {
                    // TODO: handle exception
                    Log.e(TAG,"UpdateImageAsThread "+e);
                }


            }
        };
        t.start();

    }

private Handler listAdapterHandler = new Handler()
{


    @Override
    public void handleMessage(Message msg)
    {

        switch (msg.what)
        {
            case -1:
                Log.d(TAG, "here in the handle...");
                break;
            default:
                Log.d(TAG, "here in the handle default...");
                imageAdapter.setLoadedImage(msg.what);
                //imageAdapter.notifyDataSetChanged();
                break;
        }
    }
};

Upvotes: 0

Dawid Sajdak
Dawid Sajdak

Reputation: 3084

You can use lazy load images like this:

https://github.com/thest1/LazyList

Lazy load of images in ListView

Upvotes: 0

Related Questions