我开始使用 Universal Image Loader,但不知何故我编造了一些错误。我有一个带有拇指图像的列表视图,当我向下和向上滚动时,所有图像都在混合,如果我回滚到它,顶部图像将是另一个图像,等等。
请帮助,我使用 viewHolder,我认为它会解决问题,但没有。
这是我的适配器:
public class CouponAdapter extends BaseAdapter {
public List<HashMap<String, String>> adapterData = new ArrayList<HashMap<String, String>>();
Context context;
LayoutInflater inflater;
ImageLoader imageLoader;
public CouponAdapter(Context context, List<HashMap<String, String>> data, int resource) {
imageLoader = ImageLoader.getInstance();
inflater = LayoutInflater.from(context);
setAdapterData(data);
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.kupon_list_item, null);
viewHolder = new ViewHolder();
viewHolder.titleTv = (TextView) convertView.findViewById(R.id.cpTitle);
viewHolder.imgView = (ImageView) convertView.findViewById(R.id.kuponImageView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
HashMap<String, String> map = adapterData.get(position);
viewHolder.titleTv.setText(map.get("coupon_title"));
String imgUrl = Variables.baseURL + Variables.kuponAlapKepUrl + map.get("img");
imageLoader.displayImage(imgUrl, viewHolder.imgView);
return convertView;
}
static class ViewHolder {
public TextView titleTv;
public ImageView imgView;
public String imgUrl;
}
@Override
public int getCount() {
if (adapterData != null) {
return adapterData.size();
} else {
return 0;
}
}
@Override
public Object getItem(int position) {
return adapterData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public void setAdapterData(List<HashMap<String, String>> data) {
for (int i = 0; i < data.size(); i++) {
HashMap<String, String> map = data.get(i);
this.adapterData.add(map);
}
}
}
这是我在适配器运行之前运行的 MainActivity 中的 ImageLoader 初始化。
public class MainActivity extends AlapSlidingActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DisplayImageOptions displayimageOptions = new DisplayImageOptions.Builder()
.cacheInMemory().cacheOnDisc().build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext()).defaultDisplayImageOptions(
displayimageOptions).build();
ImageLoader.getInstance().init(config);
//............................... more code here..