3

GridView用来显示ImageViews 的列表。图像是通过ImageLoader库从 Internet 加载的。我还实现了一个自定义适配器(扩展BaseAdapter):

public class ImagePreviewAdapter extends BaseAdapter {

    private Context mContext;

    private List<LastPost> mPosts = new ArrayList<LastPost>();

    private ImageLoader mImageLoader;

    private ImageLoadingListener mLoadingListener;

    public ImagePreviewAdapter(Context context, List<LastPost> posts, ImageLoadingListener listener) {
        this.mContext = context;
        this.mPosts = posts;
        this.mLoadingListener = listener;

        Activity activity = (Activity)mContext;
        this.mImageLoader = ((MeanwhileInRussia)activity.getApplication()).getImageLoader();
    }

    public int getCount() {
        return mPosts.size();
    }

    public LastPost getItem(int position) {
        return mPosts.get(position);
    }

    public long getItemId(int position) {
        LastPost post = mPosts.get(position);
        return Long.parseLong(post.getId());
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;

        // if it's not recycled, initialize some attributes
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(125, 125));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(0, 0, 0, 0);

            String imageLink = mPosts.get(position).getImageurl();
            DisplayImageOptions displayImgOptions = ImageLoaderConfigUtil
                .getDisplayImageOptions(mContext);

            if (mLoadingListener != null) {
                mImageLoader
                    .displayImage(imageLink, imageView, displayImgOptions, mLoadingListener);
           } else {
                mImageLoader.displayImage(imageLink, imageView, displayImgOptions);
           }

        } else {
            ImageView restoringView = (ImageView)convertView;
            imageView = restoringView;
        }

        return imageView;
    }
}

每次系统调用该getView()方法时,我都会创建一个新ImageView对象并要求 ImageLoader 将图像放入其中(如果 convertView == null 当然)。对于 URL 的第一部分,一切正常。但是我的问题从第二部分开始:没有新图像GridView,只有第一部分的副本......我已经意识到问题隐藏在某个地方convertView == null:对于所有理论上的新元素,此检查始终等于 FALSE!而不是创建新对象并从 URL 加载图像,我的代码将一些旧对象放入新位置。有趣的事实:getItemId(position)方法为我的 LastPost 对象返回正确的 ID。这是否意味着我在适配器中有正确的 LastPosts 对象的视图不正确?

有什么建议么?谢谢!

4

2 回答 2

3

尝试这个....

public class ImagePreviewAdapter extends BaseAdapter 
{

private Context mContext;

private List<LastPost> mPosts = new ArrayList<LastPost>();

private ImageLoader mImageLoader;

private ImageLoadingListener mLoadingListener;

/**
 * Constructor
 * 
 * @param context
 * @param posts
 * @param listener - using for showing loading progress
 */
public ImagePreviewAdapter(Context context, List<LastPost> posts, ImageLoadingListener listener) {
    this.mContext = context;
    this.mPosts = posts;
    this.mLoadingListener = listener;

    Activity activity = (Activity)mContext;
    this.mImageLoader = ((MeanwhileInRussia)activity.getApplication()).getImageLoader();
}

public int getCount() {
    return mPosts.size();
}

public LastPost getItem(int position) {
    return mPosts.get(position);
}

public long getItemId(int position) {
    LastPost post = mPosts.get(position);
    return Long.parseLong(post.getId());
}

/*
 * Create a new ImageView for each item referenced by the Adapter
 */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;

    // if it's not recycled, initialize some attributes
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(125, 125));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);

    } 
   else 
      {
        ImageView restoringView = (ImageView)convertView;
        imageView = restoringView;
     }
        final String imageLink = mPosts.get(position).getImageurl();
        final DisplayImageOptions displayImgOptions = ImageLoaderConfigUtil
            .getDisplayImageOptions(mContext);

   if (mLoadingListener != null) 
    {
       mImageLoader.displayImage(imageLink, imageView,displayImgOptions,mLoadingListener);
       }
     else 
        {
            mImageLoader.displayImage(imageLink, imageView, displayImgOptions);
       }

    return imageView;
}
}
于 2012-09-09T15:52:01.937 回答
1

尝试这个

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

   ViewHolder holder = null;
    // if it's not recycled, initialize some attributes
    if (convertView == null) {
        holder = new ViewHolder();
        holder.image = new ImageView(mContext);
        holder.image.setLayoutParams(new GridView.LayoutParams(125, 125));
        holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        holder.image.setPadding(0, 0, 0, 0);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    String imageLink = mPosts.get(position).getImageurl();
    DisplayImageOptions displayImgOptions = ImageLoaderConfigUtil.getDisplayImageOptions(mContext);
    mImageLoader.displayImage(imageLink, holder.image, displayImgOptions);

    return convertView;
}


protected class ViewHolder{
    ImageView image;
}
于 2012-09-09T14:35:32.020 回答