0

Hi guys i have a problem with my android app which it has a custom ListView with images loaded from a web server, but the images keeps repeating as i scroll down. I've searched everywhere but nothing worked for me.

public class MyListAdapter extends BaseAdapter{

protected JSONArray results;
protected Activity activity;
protected int layout;
protected Context context;
private static LayoutInflater inflater = null;

public MyListAdapter(JSONArray results, Activity activity, int layout, Context context){
    this.results = results;
    this.activity = activity;
    this.layout = layout;
    this.context = context;
    inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.results.length();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Cell cell;
    JSONObject object = null;
    try {
        object = this.results.getJSONObject(position);
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    if(convertView == null){
        cell = new Cell();
        convertView = inflater.inflate(layout, null);
        cell.image = (ImageView) convertView.findViewById(R.id.image);
        cell.title = (TextView) convertView.findViewById(R.id.title);
        Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/digital-7.ttf");
        cell.title.setTypeface(custom_font);
        try {
            cell.title.setText(object.getString("name"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            new DownloadImageTask(cell.image, object).execute("http://c2ez.ma/image.php?"+object.getString("id_product"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        convertView.setTag(cell);
    }else{
        cell = (Cell) convertView.getTag();
    }



    return convertView;
}

public String getUrl(int position) throws JSONException{
    JSONObject object = this.results.getJSONObject(position);
    return object.getString("url");
}

private static class Cell{
    protected TextView title;
    protected ImageView image;
}

private class DownloadImageTask extends AsyncTask<String, Void, String> {
      ImageView bmImage;
      JSONObject object;
      String imgUrl = null;

      public DownloadImageTask(ImageView bmImage, JSONObject object) {
          this.bmImage = bmImage;
          this.object = object;
      }



      @Override
    protected void onPreExecute() {
        bmImage.setImageResource(R.drawable.defaultimg);
    }

    protected String doInBackground(String... urls) {
        try {
            imgUrl = new Connector().GetImagePath("http://c2ez.ma/image.php?id="+object.getString("id_product"));
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
          return imgUrl;
      }

      protected void onPostExecute(String result) {
          if(result == null){
              bmImage.setImageResource(R.drawable.defaultimg);
          }else{
              Picasso.with(context).cancelRequest(bmImage); 
              Picasso.with(context).load(result).tag(context).into(bmImage);
              bmImage.setTag(imgUrl);
          }
      }
    }

@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return super.getItemViewType(position);
}

@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return super.getViewTypeCount();
}

}

I've also tried moving the code for assigning values to the views outside the if statement i think it worked but they show after a very long time.

EDIT : When I saw my logcat I found out that when i scroll down it takes a lot of time before it starts downloading the next images and its displaying a message AsyncTask #5 calls() detach.

Here's the logcat : When it displays the urls it means that they're downloaded.

06-27 13:09:38.821: I/System.out(10823): KnoxVpnUidStorageknoxVpnSupported API value returned is false
06-27 13:09:39.111: V/image(10823): http://c2ez.ma/img/p/2/4/24.jpg
06-27 13:09:39.131: W/Settings(10823): Setting airplane_mode_on has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
06-27 13:09:39.151: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:39.361: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:39.361: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:39.371: V/image(10823): http://c2ez.ma/img/p/824-858.jpg
06-27 13:09:39.381: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:39.611: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:39.611: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:39.631: V/image(10823): http://c2ez.ma/img/p/1402-1428.jpg
06-27 13:09:39.651: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:39.861: I/System.out(10823): AsyncTask #2 calls detatch()
06-27 13:09:39.861: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:39.871: V/image(10823): http://c2ez.ma/img/p/1405-1431.jpg
06-27 13:09:39.931: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:40.111: I/System.out(10823): AsyncTask #2 calls detatch()
06-27 13:09:40.111: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:40.121: V/image(10823): http://c2ez.ma/img/p/1406-1432.jpg
06-27 13:09:40.121: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:40.361: I/System.out(10823): AsyncTask #2 calls detatch()
06-27 13:09:40.361: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:40.361: V/image(10823): http://c2ez.ma/img/p/1407-1433.jpg
06-27 13:09:40.381: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:40.591: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:40.591: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:40.591: V/image(10823): http://c2ez.ma/img/p/1408-1434.jpg
06-27 13:09:40.651: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:09:40.791: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:40.991: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:41.181: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:41.381: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:41.791: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:41.991: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:42.191: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:42.401: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:42.611: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:42.821: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:43.021: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:43.251: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:43.461: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:43.671: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:43.871: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:44.071: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:44.271: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:44.461: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:44.671: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:44.871: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:45.071: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:45.281: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:45.471: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:45.681: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:45.891: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:46.111: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:46.301: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:46.491: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:46.701: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:46.901: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:47.091: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:47.301: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:47.491: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:47.711: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:47.911: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:48.111: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:09:48.311: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:48.511: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:48.641: D/ViewRootImpl(10823): ViewPostImeInputStage ACTION_DOWN
06-27 13:09:48.711: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:48.951: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:49.151: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:49.351: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:49.551: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:49.601: D/ViewRootImpl(10823): ViewPostImeInputStage ACTION_DOWN
06-27 13:09:49.751: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:49.971: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:50.171: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:50.371: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:50.571: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:50.771: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:50.981: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:51.171: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:51.371: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:51.561: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:51.771: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:51.961: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:52.161: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:52.371: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:52.591: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:52.791: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:53.001: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:53.201: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:53.391: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:53.611: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:53.841: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:54.051: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:54.251: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:54.501: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:54.701: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:54.891: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:55.111: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:55.321: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:55.531: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:55.761: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:55.961: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:56.171: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:56.361: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:56.571: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:56.761: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:56.961: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:57.211: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:57.451: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:57.661: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:57.861: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:58.091: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:09:58.301: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:10:01.631: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:10:01.911: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:10:02.141: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:10:02.381: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:10:02.851: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:10:03.081: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:10:03.301: I/System.out(10823): AsyncTask #5 calls detatch()
06-27 13:10:03.511: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:03.711: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:03.921: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:04.131: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:04.351: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:04.551: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:04.761: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:04.991: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:05.201: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:05.391: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:05.601: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:05.801: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:06.011: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:06.221: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:06.421: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:06.641: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:06.851: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:07.051: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:07.251: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:07.451: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:07.661: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:07.911: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:08.111: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:08.311: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:08.511: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:08.711: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:08.961: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:09.241: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:09.241: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:09.251: V/image(10823): http://c2ez.ma/img/p/1409-1435.jpg
06-27 13:10:09.251: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:09.521: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:09.521: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:09.531: V/image(10823): http://c2ez.ma/img/p/1410-1436.jpg
06-27 13:10:09.551: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:09.801: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:09.801: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:09.821: V/image(10823): http://c2ez.ma/img/p/1411-1437.jpg
06-27 13:10:09.851: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:10.031: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:10.251: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:10.451: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:10.651: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:10.851: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:11.051: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:11.261: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:11.491: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:11.491: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:11.511: V/image(10823): http://c2ez.ma/img/p/1412-1438.jpg
06-27 13:10:11.521: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:11.771: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:11.771: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:11.781: V/image(10823): http://c2ez.ma/img/p/1413-1439.jpg
06-27 13:10:11.791: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:12.021: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:12.021: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:12.031: V/image(10823): http://c2ez.ma/img/p/1414-1440.jpg
06-27 13:10:12.061: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:12.241: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:12.451: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:12.651: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:12.871: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:13.071: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:13.271: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:13.481: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:13.691: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:14.891: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:15.101: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:15.301: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:15.521: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:15.721: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:15.921: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:16.171: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:16.171: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:16.181: V/image(10823): http://c2ez.ma/img/p/1415-1441.jpg
06-27 13:10:16.181: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:16.421: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:16.621: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:17.821: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:18.021: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:18.221: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:18.411: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:18.611: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:19.831: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:19.831: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:19.841: V/image(10823): http://c2ez.ma/img/p/1416-1442.jpg
06-27 13:10:19.841: I/System.out(10823): (HTTPLog)-Static: isSBSettingEnabled false
06-27 13:10:20.051: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:20.251: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:20.441: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:20.631: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:20.841: I/System.out(10823): AsyncTask #1 calls detatch()
06-27 13:10:21.041: I/System.out(10823): AsyncTask #1 calls detatch()

EDIT : the issue is gone but now it takes a very long time loading the image into the imageview about 30 sec or more even though the image is downloaded.

Tomahawk
  • 78
  • 11
  • 1
    hint: you get only one null `convertView` – Blackbelt Jun 27 '16 at 13:11
  • Possible duplicate of [Using Picasso library with ListView](http://stackoverflow.com/questions/23120238/using-picasso-library-with-listview) – Hitesh Sahu Jun 27 '16 at 13:18
  • 1
    Why you are storing bitmap in hashmap, Picasso handle image caching and loading in offline mode. And why you are using async task to download bitmap and then again using Picasso to download image? – Hitesh Sahu Jun 27 '16 at 13:23
  • I forgot to delete my code because i was doing the loading manually and then i used picasso and i left it there, i've edited my code above. – Tomahawk Jun 27 '16 at 13:31

2 Answers2

1

You pass a cell.image to the DownloadImageTask. The list row view is recycled (the same view is used multiple times, you only fill the rows). If you pass a reference to the part of the view, the same reference is added to all AsyncTasks and when the image is downloaded all rows are filled at one time.

Suggestions:

  1. Use exact list position insead of ImageView to be filled.
  2. Use Picasso library, and You will have no more problems with images on list.
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
0

Change your getView() as following and check then:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Cell cell;
JSONObject object = null;
try {
    object = this.results.getJSONObject(position);
} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

if(convertView == null){
    cell = new Cell();
    convertView = inflater.inflate(layout, null);
    cell.image = (ImageView) convertView.findViewById(R.id.image);
    cell.title = (TextView) convertView.findViewById(R.id.title);
    Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/digital-7.ttf");
    cell.title.setTypeface(custom_font);
    try {
        cell.title.setText(object.getString("name"));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    convertView.setTag(cell);
}else{
    cell = (Cell) convertView.getTag();
}



try {
  String imgUrl = "http://c2ez.ma/image.php?     id="+object.getString("id_product");
  Picasso.with(context).load(imgUrl).into(cell.image);
  cell.image.setTag(imgUrl);
  } catch (JSONException e) {
    // TODO Auto-generated catch block
      e.printStackTrace();
  }
    return convertView;
    }
Uniruddh
  • 4,427
  • 3
  • 52
  • 86
  • That's exactly what im doing its just that i don't know the exact url of the images thats why i'm getting them with a web service in an AsyncTask – Tomahawk Jun 27 '16 at 13:46
  • In that case do one thing. First of all please fetch all the data and image urls and set them in a model class. In between show some loading view. Once you have fetched all data then set it in listview. That way it will be more user friendly. Its good practice to remove any heavy tasks from adapter's `getView()`. – Uniruddh Jun 27 '16 at 13:56
  • i also tried that but it will take a very long time because i have thousands of images. For now everything is good except one issue that even though the bitmaps are already downloaded they don't get loaded into the imageview as soon as they get downloaded they take some time. Is it normal? Any idea? – Tomahawk Jun 27 '16 at 14:04
  • 1
    Then you should use lazy loading for loading limited numbers of record at first, and loading more when user scrolls down. And your images are taking time as much Picasso will take to fetch them once you get url in `AsyncTask`. – Uniruddh Jun 27 '16 at 14:08