在通用图像加载器示例应用中的 ListView

7
我正在使用通用图像加载器来显示列表视图中的缩略图。我已在适配器中实现了基本步骤,一切都正常工作。问题是,如果我在列表中有10个项目,当我向下滚动时,会显示新的图像,但当我向上滚动时,这些图像会再次被替换为正确的图像。我希望最初显示的前5个图像不被替换。这样,当我向上和向下滚动时,图像就不会一遍又一遍地被替换。
就像在UIL示例应用程序ListActivity中所做的那样,一旦加载了图像,当向上滚动时就不会被替换。
以下是代码:
应用程序类
public class MyApplication extends Application {

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    // Create default options which will be used for every 
   //  displayImage(...) call if no options will be passed to this method
    DisplayImageOptions displayimageOptions = new DisplayImageOptions.Builder().cacheInMemory().cacheOnDisc().build();


    // Create global configuration and initialize ImageLoader with this configuration
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).
            defaultDisplayImageOptions(displayimageOptions).build();
    ImageLoader.getInstance().init(config);
}
}

Adapter

public class CarListAdapter extends BaseAdapter {

private ArrayList<CarDetail> items = new ArrayList<CarDetail>();
private Context context;
private ImageLoader imageLoader;
private ImageLoadingListener animateFirstDisplayListener;



public CarListAdapter(Context context , ArrayList<CarDetail> items , ImageLoadingListener animateFirstDisplayListener) {
    super();
    this.context = context;
    this.items = items;
    this.animateFirstDisplayListener = animateFirstDisplayListener;
    imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(context));

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return items.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Log.d("Inside", "GetView");

    LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder = null;
    final CarDetail car = items.get(position);


     if (convertView == null) {

            convertView = mInflater.inflate(R.layout.car_list_row, parent , false);
            holder = new ViewHolder();
            holder.tvCarName = (TextView) convertView.findViewById(R.id.tvCarName);
            holder.tvDailyPriceValue = (TextView) convertView.findViewById(R.id.tvDailyPriceValue);
            holder.tvWeeklyPriceValue = (TextView) convertView.findViewById(R.id.tvWeeklyPriceValue);
            holder.tvWeekendPriceValue = (TextView) convertView.findViewById(R.id.tvWeekendPriceValue);
            holder.imgCar = (ImageView) convertView.findViewById(R.id.imgCar);
            holder.btnBookNow = (Button) convertView.findViewById(R.id.btnBookNow);

            holder.btnBookNow.setFocusable(false);
            holder.btnBookNow.setFocusableInTouchMode(false);

            holder.btnBookNow.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(context , BookingFormActivity.class);
                    intent.putExtra("car_name", car.getCarName());
                    intent.putExtra("min_age", car.getMinimumAge());
                    context.startActivity(intent);
                }
            });

            convertView.setTag(holder);
    }
     else {

         holder = (ViewHolder) convertView.getTag();
        }

     holder.tvCarName.setText(car.getCarName());
     holder.tvDailyPriceValue.setText(car.getDailyPrice());
     holder.tvWeeklyPriceValue.setText(car.getWeeklyPrice());
     holder.tvWeekendPriceValue.setText(car.getWeekendPrice());
     imageLoader.displayImage(car.getThumbUrl(), holder.imgCar, animateFirstDisplayListener);


  return convertView;
 }

 static class ViewHolder {

            TextView tvCarName;
            TextView tvDailyPriceValue;
            TextView tvWeeklyPriceValue;
            TextView tvWeekendPriceValue;
            ImageView imgCar;
            Button btnBookNow;
        }




}

我建议您使用懒加载列表适配器。 - blganesh101
任何链接都会有所帮助。 - Ravi
2个回答

5
您正在两次调用init(),第二次调用将覆盖您的缓存选项。请删除此行代码:
imageLoader.init(ImageLoaderConfiguration.createDefault(context));

它会抛出一个异常:java.lang.IllegalStateException: 在使用之前必须使用配置初始化ImageLoader - Ravi
这意味着你的应用类的onCreate()中的第一个init()没有被调用。请确认你在清单文件中将MyApplication.java定义为应用类。 - Nachi

1

我看到了这个链接,虽然我没有尝试过,但它应该可以工作,因为它不会重复使用视图,所以就不会有这个问题,尽管它不是内存高效的。无论如何,谢谢。 - Ravi
这个示例无法从链接获取图像。为什么会发生这种情况?我调试了一下,发现它在内存缓存中失败了。 - alicanbatur

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接