Skip to content Skip to sidebar Skip to footer

Downloads Multiple Images In The Same Time, And Update The List View

I am developing an application in android that will get the latest news from a server service and then display in a list view. The code is as follows: HomeFragment.java @Suppress

Solution 1:

use this library UrlImageViewHelper

by calling

UrlImageViewHelper.setUrlDrawable(yourImageView, "http://example.com/image.png", R.drawable.your_loading_image);

in your adapter,
it will create the list first and download the images later, when the image is downloading, it will show your_loading_image, and the downloaded image will be shown shortly

edit:
to be precise, the Image returned by yout getImage() function in the class NewsItem should be a String url to your image, and replace this line

holder.news_image.setImageBitmap(((NewsItem) news_data.get(position)).getImage());

to

UrlImageViewHelper.setUrlDrawable(holder.news_image, news_data.get(position).getImage(), R.drawable.your_loading_image);


i've met this problem too, and UrlImageViewHelper is the easiest approach to solve this, just import the library and use one line and you get what you want, no need to define your own AsyncTask as the library itself using AsyncTask to load the images


Solution 2:

I recommend you using AsyncTask in Android. Please have a look at http://developer.android.com/reference/android/os/AsyncTask.html

This will help you run in the background and Publish your progress or downloaded image onto the imageview.

new AsyncTask<String,Void,Void>(){
@Override
protected Void doInBackground(String...arg){
   //download the image against the url, and run in the background as not on UI thread.
}

@Override
protected void onPostExecute(Void v){
    //Load the imageview with the downloaded image.
}

}.execute(url1,url2,url3);

Post a Comment for "Downloads Multiple Images In The Same Time, And Update The List View"