2012-09-09 46 views
3

我使用GridView來顯示ImageView的列表。圖像通過ImageLoader庫從Internet加載。我也實現了自定義適配器(延伸BaseAdapter):使用ImageLoader庫時GridView中的重複項目

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 ==當然空) 。對於URL的第一部分,一切正常。但我的問題從第二部分開始:GridView中沒有新圖像,只是第一部分的副本...我意識到這個問題隱藏在convertView == null的某處:對於理論上所有新元素,這種檢查總是等於FALSE!而不是創建新的對象並從URL加載圖像,我的代碼將一些舊對象放到新的位置。有趣的事實:getItemId(position)方法返回我的LastPost對象的正確ID。這是否意味着我只是在正確的LastPosts對象的適配器中有不正確的視圖?

有什麼建議嗎?謝謝!

回答

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

謝謝,希瓦吉!它的工作原理:) – fraggjkee

+0

如何訪問imageLink在該位置,因爲它被包含在第一個條件 –

+0

什麼是同時InRussia?在這一行this.mImageLoader =((AlongInRussia)activity.getApplication())。getImageLoader(); –

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

'convertView.setTag(持有者)'提供了一個強制關閉,因爲convertView將alwasy空在這個地方 – fraggjkee