【问题标题】:Rows keeps repeating in the ListView行在 ListView 中不断重复
【发布时间】:2016-06-27 21:58:42
【问题描述】:

大家好,我的 android 应用程序有问题,它有一个自定义 ListView,其中包含从 Web 服务器加载的图像,但是当我向下滚动时图像不断重复。我到处搜索,但没有什么对我有用。

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();
}

}

我还尝试将用于将值分配给 if 语句之外的视图的代码移动我认为它有效但它们在很长一段时间后显示。

编辑:当我看到我的 logcat 时,我发现当我向下滚动时,它需要很长时间才能开始下载下一个图像并显示一条消息 AsyncTask #5 calls() detach。

这是 logcat : 当它显示网址时,表示它们已下载。

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()

编辑:问题消失了,但现在即使下载了图像,也需要很长时间将图像加载到 imageview 中大约 30 秒或更长时间。

【问题讨论】:

  • 提示:你只会得到一个 null convertView
  • 为什么要将位图存储在 hashmap 中,Picasso 在离线模式下处理图像缓存和加载。为什么你使用异步任务下载位图,然后再次使用毕加索下载图像?
  • 我忘了删除我的代码,因为我是手动加载的,然后我使用了 picasso,我把它留在那里,我在上面编辑了我的代码。

标签: android json image listview picasso


【解决方案1】:

您将cell.image 传递给DownloadImageTask。列表行视图被回收(同一个视图被多次使用,你只填充行)。如果您传递对视图部分的引用,则会将相同的引用添加到所有 AsyncTask 中,并且在下载图像时一次性填充所有行。

建议:

  1. 使用ImageView 中的确切列表位置进行填充。
  2. 使用Picasso库,列表中的图片不会再有问题了。

【讨论】:

  • 我已经在使用毕加索了,我会尝试第一个建议并告诉你。
【解决方案2】:

如下更改您的getView(),然后检查:

@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;
    }

【讨论】:

  • 这正是我所做的,只是我不知道图像的确切 url,这就是为什么我在 AsyncTask 中使用 Web 服务来获取它们
  • 在这种情况下做一件事。首先,请获取所有数据和图像 url 并将它们设置在模型类中。在两者之间显示一些加载视图。获取所有数据后,将其设置在列表视图中。这样,它将更加用户友好。从适配器的getView() 中删除任何繁重的任务是一种很好的做法。
  • 我也试过了,但这需要很长时间,因为我有数千张图片。现在一切都很好,除了一个问题,即使位图已经下载,它们不会在下载后立即加载到图像视图中,它们需要一些时间。正常吗?有什么想法吗?
  • 那么你应该使用延迟加载来加载有限数量的记录,当用户向下滚动时加载更多。一旦您在AsyncTask 中获得网址,您的图片将花费毕加索获取它们的时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 2023-03-07
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
  • 2017-04-22
相关资源
最近更新 更多