0

i'm a beginner with Java and i would like to display web images on a ListView from JSON. For now i've create ListView with text strings and local image (mipmap.ic_launcher). Also loading img from web works, but only from onCreate using

mImageView = (ImageView) findViewById(R.id.image);
new LoadImageTask(this).execute(KEY_URL);

Rest of my code:

public void onLoaded(List<AndroidVersion> androidList) {
    for (AndroidVersion wykopList : androidList) {

        HashMap<String, String> map = new HashMap<>();
        mImageView = (ImageView) findViewById(R.id.image);
        new LoadImageTask(this).execute(KEY_URL);
        map.put(KEY_VER, wykopList.getVer());
        map.put(KEY_NAME, wykopList.getName());
        map.put(KEY_API, wykopList.getApi());
        map.put("image",String.valueOf(R.mipmap.ic_launcher));


        mAndroidMapList.add(map);
    }

    loadListView();
}



private void loadListView() {
    ListAdapter adapter = new SimpleAdapter(MainActivity.this, mAndroidMapList, R.layout.list_item,
            new String[] { KEY_VER, KEY_NAME, KEY_API,"image" },
            new int[] { R.id.version,R.id.name, R.id.api,R.id.image});


    mListView.setAdapter(adapter);

}

@Override
public void onImageLoaded(Bitmap bitmap) {

    mImageView.setImageBitmap(bitmap);
}
Szmerd
  • 499
  • 1
  • 4
  • 13
  • instead you may use Universal Image Loader. that pretty easy, options are also for you like picasso , glide. – Prashant Oct 17 '16 at 10:29
  • You can use [Picasso](http://square.github.io/picasso/) for image loading, Picasso.with(context).load("Image_url").into(imageView); – Adnan Bin Mustafa Oct 17 '16 at 10:33

2 Answers2

1

Universal Image Loader is good than getting bitmap for every image. please follow the step.

step 1- download universal image loader from here https://github.com/nostra13/Android-Universal-Image-Loader

step 2- put this jar file on libs folder in your project. and also add dependency in you build.gradle file

step 3- instantiate ImageLoader and DisplayImageOptions.

ImageLoader imageLoader = ImageLoader.getInstance(); DisplayImageOptions imageDisplayOptions = new DisplayImageOptions.Builder() .cacheInMemory(true).cacheOnDisk(true) .showImageOnLoading(R.drawable.umg_player_img) .bitmapConfig(Bitmap.Config.ARGB_8888) .build();

step 4- Now finally set your image on image view imageLoader.displayImage(imageUrl, mImageView, imageDisplayOptions);

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
0

Use picasso for retrieving the image from the server.

This is the gradle dependency to use picasso in your project :-

compile 'com.squareup.picasso:picasso:2.5.2'

And this is the sample code how to use it :-

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);
LoveAndroid
  • 186
  • 1
  • 11