0

我正在为列表视图中的每一行加载不同的图像,我查看了几个教程并观看了有关此最佳实践的谷歌视频,但它仍然无法正常工作。我的列表视图项目最终以幻灯片形式显示所有图像,并且当我滚动列表时它们也会发生变化。

我正在使用通用图像加载器来下载和缓存我的图像。这是我整个班级的代码。我似乎无法让它发挥作用。

public class EventAdapter extends ArrayAdapter<EventDetails>{

private Context context;
LayoutInflater inflater;
LinkedList<EventDetails> event_details;
ImageView event_imge;
ViewHolder holder;
public ImageLoader imageLoader;


public EventAdapter(Context mcontext,
        LinkedList<EventDetails> m_event_details) {
    super(mcontext, R.layout.event_list_row, m_event_details);

    this.context = mcontext;
    this.event_details = new LinkedList<EventDetails>();
    this.event_details = m_event_details;
}   

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

    View v = myview;

    if (inflater == null) {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    if (v == null) {
        v = inflater.inflate(R.layout.event_list_row, parent,
                false);
        imageLoader = ImageLoader.getInstance();
        holder = new ViewHolder();
        holder.event_name = (TextView) v.findViewById(R.id.event_name);
        holder.event_show = (TextView) v.findViewById(R.id.event_show);
        holder.event_time = (TextView) v.findViewById(R.id.event_time);
        holder.event_date = (TextView) v.findViewById(R.id.event_date);
        holder.event_genre = (TextView) v.findViewById(R.id.event_genre);
        holder.event_price = (TextView) v.findViewById(R.id.event_price);
        holder.img = (ImageView) v.findViewById(R.id.event_image);
        holder.event_ll = (LinearLayout) v.findViewById(R.id.event_frame_ll);

        v.setTag(holder);
    }else{
        holder = (ViewHolder) v.getTag();
    }

    final EventDetails eD =  event_details.get(position);
    if(eD != null){
        holder.event_name.setText(eD.name.toUpperCase());
        holder.event_show.setText(eD.show.toUpperCase());
        holder.event_time.setText(eD.time.toUpperCase());
        holder.event_date.setText(eD.date.toUpperCase());
        holder.event_price.setText("£"+String.format("%.2f", eD.price));
        holder.event_genre.setText(eD.genre.toUpperCase());

        if(eD.isStandBy == 0){
            holder.event_ll.setVisibility(View.GONE);
        }else{
            holder.event_ll.setVisibility(View.VISIBLE);
        }

        holder.img.setTag(eD.image_url[0]);
        if(MemoryCacheUtil.findCachedBitmapsForImageUri(eD.image_url[0], ImageLoader.getInstance().getMemoryCache()).size() > 0){
            //holder.img.setBackgroundDrawable(new BitmapDrawable(MemoryCacheUtil.findCachedBitmapsForImageUri(eD.image_url[0], ImageLoader.getInstance().getMemoryCache()).get(0)));
            holder.img.setImageBitmap(MemoryCacheUtil.findCachedBitmapsForImageUri(event_details.get(position).image_url[0], ImageLoader.getInstance().getMemoryCache()).get(0));

        }else{              
            DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).build();
            ImageLoader.getInstance().loadImage(eD.image_url[0], options, new SimpleImageLoadingListener() {                            

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    //holder.img.setBackgroundDrawable(new BitmapDrawable(loadedImage));
                    holder.img.setImageBitmap(loadedImage);
                }
            });
        }
    }       

    return v;
}

static class ViewHolder{

    TextView event_name;
    TextView event_show;
    TextView event_time;
    TextView event_date;
    TextView event_genre;
    TextView event_price;
    ImageView img;
    LinearLayout event_ll;
}
4

1 回答 1

0

holder是一个类变量。因此,只有一个 holder 实例附加到您的每个回收视图。这应该有效。

另外,不要LinkedList用来存放物品。的访问时间LinkedListO(n)。改为使用ArrayList

public View getView(int position, View myview, ViewGroup parent) {
    ViewHolder holder;

    View v = myview;

    if (inflater == null) {
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    if (v == null) {
        v = inflater.inflate(R.layout.event_list_row, parent,
                false);

        imageLoader = ImageLoader.getInstance();
        holder = new ViewHolder();
        holder.event_name = (TextView) v.findViewById(R.id.event_name);
        holder.event_show = (TextView) v.findViewById(R.id.event_show);
        holder.event_time = (TextView) v.findViewById(R.id.event_time);
        holder.event_date = (TextView) v.findViewById(R.id.event_date);
        holder.event_genre = (TextView) v.findViewById(R.id.event_genre);
        holder.event_price = (TextView) v.findViewById(R.id.event_price);
        holder.img = (ImageView) v.findViewById(R.id.event_image);
        holder.event_ll = (LinearLayout) v.findViewById(R.id.event_frame_ll);

        v.setTag(holder);
    }else{
        holder = (ViewHolder) v.getTag();
    }

    final EventDetails eD =  event_details.get(position);
    if(eD != null){
        holder.event_name.setText(eD.name.toUpperCase());
        holder.event_show.setText(eD.show.toUpperCase());
        holder.event_time.setText(eD.time.toUpperCase());
        holder.event_date.setText(eD.date.toUpperCase());
        holder.event_price.setText("£"+String.format("%.2f", eD.price));
        holder.event_genre.setText(eD.genre.toUpperCase());

        if(eD.isStandBy == 0){
            holder.event_ll.setVisibility(View.GONE);
        }else{
            holder.event_ll.setVisibility(View.VISIBLE);
        }

        holder.img.setTag(eD.image_url[0]);
        if(MemoryCacheUtil.findCachedBitmapsForImageUri(eD.image_url[0], ImageLoader.getInstance().getMemoryCache()).size() > 0){
            //holder.img.setBackgroundDrawable(new BitmapDrawable(MemoryCacheUtil.findCachedBitmapsForImageUri(eD.image_url[0], ImageLoader.getInstance().getMemoryCache()).get(0)));
            holder.img.setImageBitmap(MemoryCacheUtil.findCachedBitmapsForImageUri(event_details.get(position).image_url[0], ImageLoader.getInstance().getMemoryCache()).get(0));

        }else{              
            DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).build();
            ImageLoader.getInstance().loadImage(eD.image_url[0], options, new SimpleImageLoadingListener() {                            

                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    //holder.img.setBackgroundDrawable(new BitmapDrawable(loadedImage));
                    holder.img.setImageBitmap(loadedImage);
                }
            });
        }
    }       

    return v;
}
于 2013-07-23T09:44:45.900 回答