在列表视图中加载图像不能正常工作

时间:2013-07-22 22:24:49

标签: android android-listview android-view universal-image-loader android-adapterview

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

我正在使用Universal Image Loader下载和缓存我的图像。这是我整个班级的代码。我似乎无法让这个工作。

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;
}

1 个答案:

答案 0 :(得分:0)

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

另外,请勿使用LinkedList来存储项目。 LinkedList的访问时间为O(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;
}