ali Atwa ali Atwa - 1 year ago 76
JSON Question

caching images from json in listview?

I followed this lesson here which talk about json parsing and image loading in list view the example works good but it has a problem in imageview when scrolling up & down all images reloading so can anyone solve this problem?

this the adapter of listview

package com.wingnity.jsonparsingtutorial;

import java.util.ArrayList;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ActorAdapter extends ArrayAdapter<Actors> {
ArrayList<Actors> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;

public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
actorList = objects;

public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(;
holder.tvName = (TextView) v.findViewById(;
holder.tvDescription = (TextView) v.findViewById(;
holder.tvDOB = (TextView) v.findViewById(;
holder.tvCountry = (TextView) v.findViewById(;
holder.tvHeight = (TextView) v.findViewById(;
holder.tvSpouse = (TextView) v.findViewById(;
holder.tvChildren = (TextView) v.findViewById(;
} else {
holder = (ViewHolder) v.getTag();
new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
holder.tvDOB.setText("B'day: " + actorList.get(position).getDob());
holder.tvHeight.setText("Height: " + actorList.get(position).getHeight());
holder.tvSpouse.setText("Spouse: " + actorList.get(position).getSpouse());
holder.tvChildren.setText("Children: " + actorList.get(position).getChildren());
return v;


static class ViewHolder {
public ImageView imageview;
public TextView tvName;
public TextView tvDescription;
public TextView tvDOB;
public TextView tvCountry;
public TextView tvHeight;
public TextView tvSpouse;
public TextView tvChildren;


private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;

public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new;
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
return mIcon;

protected void onPostExecute(Bitmap result) {

Answer Source

Used the Android Universal Image-Loader This is best


    private ImageLoader imageLoader1;

On Create

 imageLoader1 = ImageLoader.getInstance();


no_image here a drawable image without any image load in Cache

            options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.no_image) // resource or drawable
            .showImageForEmptyUri(R.drawable.no_image) // resource or drawable

    imageLoader1.displayImage(yourpath.replace(" ", "%20"), ivprofile, options);

You Can Use Piccaso Also and Glide also

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download