图像不保留 DataViewAdapter

Image not persisting DataViewAdapter

我有一个要显示的图像列表。我正在成功获取图像并将图像设置到 ImageView。

但问题是,每当我滚动图像列表时,图像视图往往会加载其他图像视图的图像。即随机更改 imageview 的图像。

我希望一旦图像被加载,就会被持久化到它们各自的图像视图中。

public View getView(int position, View convertView, ViewGroup parent) {

    dish = dishes.get(position);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.dish_summary, parent,
            false);
    image = (ImageView) rowView.findViewById(R.id.dish_image);
    Log.i("image",String.valueOf(image.getTag()));
    if (image.getTag() == null){
    new LoadImage().execute(dish.getImage());   
    }       
    notifyDataSetChanged(); 
    return rowView;
}


private class LoadImage extends AsyncTask<String, String, Bitmap> {
    @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(context);
            pDialog.setMessage("Loading Menu ....");
            pDialog.show();
    }
       protected Bitmap doInBackground(String... args) {
         try {
              bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
        } catch (Exception e) {
              e.printStackTrace();
        }
      return bitmap;
       }
       protected void onPostExecute(Bitmap img) {

         if(img != null)
         {
          image.setImageBitmap(img);
          image.setTag(img);
          pDialog.dismiss();
         }else
         {
           pDialog.dismiss();
           Toast.makeText(context, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
         }
       }
}

因为每当 List 离开视线时,就会调用 GetView() 方法来回收/重新绘制 List。图像尝试重绘自身。

为了解决我的问题,我使用了外部库 SmartImageView,它允许从 URL 加载图像并将图像缓存到内存和磁盘以实现超快速加载。

希望对您有所帮助!!