我开始使用Universal Image Loader,但我以某种方式编造了一些错误。 我有一个带有拇指图像的listView,当我向下滚动所有图像混合时,顶部图像将是另一个,如果我滚动回来,等等。
请帮助,我使用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..
答案 0 :(得分:0)
您必须将下载的图像缓存在存储SD卡中,然后将其绑定到图像列表视图或Gridview。