0

I got image download code from, http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html what puzzled me is how he got the imageview's view back from the download() without return value from ImageDownloader?

public class ImageAdapter extends BaseAdapter {
...
 public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            view = new ImageView(parent.getContext());
            view.setPadding(6, 6, 6, 6);
        }

        imageDownloader.download(URLS[position], (ImageView) view);

        return view;
    }
..


public class ImageDownloader {
  ...
   private void forceDownload(String url, ImageView imageView) {
    ....
     case CORRECT:
                    task = new BitmapDownloaderTask(imageView);
                    DownloadedDrawable downloadedDrawable = new DownloadedDrawable(task);
                    imageView.setImageDrawable(downloadedDrawable);
                    imageView.setMinimumHeight(156);
                    task.execute(url);
james
  • 37
  • 2
  • 10

2 Answers2

1

If you look inside BitmapDownloadTask class, there is imageView.setImageBitmap(bitmap); on OnPostExecute method which sets bitmap once it is downloaded.

Note that OnPostExecute method is called once the image is downloaded.

EDIT: When you are calling download, you are passing reference of ImageView object as a parameter. So when that method is making changes such as setting image, it is doing same to the object that is passed.

Objects are passed as reference in java. So both ImageView are referencing to the same object.

Hope it helps!

Bipin Bhandari
  • 2,694
  • 23
  • 38
  • I understand the setImageBitmap and the trigger of OnPostExexcute. How the imageView is return to the calling function. Could it be the view is passed as reference? – james Apr 17 '14 at 00:09
0

I did some search on java.

Pass by reference in java?

http://www.yoda.arachsys.com/java/passing.html

It looks like imageview is passed in as object, hence, it passed in as reference. Thanks for helping.

Community
  • 1
  • 1
james
  • 37
  • 2
  • 10