2016-05-06 119 views
1

我想在ListView中顯示具有適配器的項目,但是當在ArrayAdapter中調用getView時,它顯示良好的圖像,但在滾動時不顯示好項目。這就好像findViewById沒有給我佈局的好身份。Android ListView getView隨機顯示圖像

public class ItemPackAdapter extends ArrayAdapter<Pack> { 
Context context; 

public ItemPackAdapter(Context context, ArrayList<Pack> pack) { 
    super(context, 0, pack); 
    this.context = context; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    // Check if an existing view is being reused, otherwise inflate the view 
    final Pack pack = getItem(position); 
    if (convertView == null) { 
     convertView = LayoutInflater.from(context).inflate(R.layout.item_pack, parent, false); 
     holder = new ViewHolder(); 
     holder.textView1 = (TextView) convertView.findViewById(R.id.textView1); 
     holder.textView2 = (TextView) convertView.findViewById(R.id.textView2); 
     holder.textView3 = (TextView) convertView.findViewById(R.id.textView3); 
     holder.imageView = (ImageView) convertView.findViewById(R.id.imageView); 
     if (!pack.getImageName().equals("null")) { 
      UrlGenerator urlGenerator = new UrlGenerator(); 
      String url = urlGenerator.getDownloadPicture(pack.getImageName()); 
      DownloadPicture downloadPicture = new DownloadPicture(holder.imageView, url, getContext()); 
      downloadPicture.start(); 
     } 
     convertView.setTag(holder); 
    } 
    else { 
     holder = (ViewHolder)convertView.getTag(); 
    } 
    holder.textView1.setText(pack.getSomething1()); 
    holder.textView2.setText(pack.getSomething2()); 
    holder.textView3.setText(pack.getSomething3()); 
    if (!pack.getImageName().equals("null")) { 
     UrlGenerator urlGenerator = new UrlGenerator(); 
     String url = urlGenerator.getDownloadPicture(pack.getImageName()); 
     DownloadPicture downloadPicture = new DownloadPicture(holder.imageView, url, getContext()); 
     downloadPicture.start(); 
    } 
    if(pack.getImageName().equals("null")){ 
     holder.imageView.setImageBitmap(null); 
    } 
    return convertView; 
} 
static class ViewHolder { 
    TextView textView1; 
    TextView textView2; 
    TextView textView3; 
    ImageView imageView; 
} 
} 

其實我發現了一個解決方案:

if(pack.getImageName().equals("null")){ 
     holder.imageView.setImageBitmap(null); 
    } 

但是,當我上的ListView滾動,我可以看到在錯誤的項目形象,我需要再次滾動到getView來電刪除以前的條件的圖像。 我想要更清潔的東西:p

謝謝你提前。 對不起,我的英語不好。

+1

問題是由於意見回收。您可以使用「畢加索」或任何其他圖像加載庫來避免此問題。 –

回答

0

在這種情況下,如果你不緩存圖像,可能會給你這樣的問題。 使用強烈建議使用Picasso庫以避免此問題。在您的項目中添加picasso庫,然後編寫您的代碼這樣的事情。

Picasso.with(getApplicationContext()).load(url).into(holder.imageView); 

,而不是這一行

DownloadPicture downloadPicture = new DownloadPicture(holder.imageView, url, getContext()); 
downloadPicture.start(); 
+0

我不知道這個庫,謝謝 – gfitas

1

你的代碼看起來將工作得很好,只需稍作調整即可。我發現你兩次設置ImageView的,所以我編輯代碼如下所示

public class ItemPackAdapter extends ArrayAdapter<Pack> { 
    Context context; 
    ArrayList<Pack> packs; 

    public ItemPackAdapter(Context context, ArrayList<Pack> packs) { 
     super(context, 0, packs); 
     this.context = context; 
     this.packs = packs; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     // Check if an existing view is being reused, otherwise inflate the view 
     final Pack pack = packs.get(position); 
     if (convertView == null) { 
      convertView = LayoutInflater.from(context).inflate(R.layout.item_pack, parent, false); 
      holder = new ViewHolder(); 
      holder.textView1 = (TextView) convertView.findViewById(R.id.textView1); 
      holder.textView2 = (TextView) convertView.findViewById(R.id.textView2); 
      holder.textView3 = (TextView) convertView.findViewById(R.id.textView3); 
      holder.imageView = (ImageView) convertView.findViewById(R.id.imageView); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     if (pack.getImageName().equals("null")) { 
      holder.imageView.setImageBitmap(null); 
     } else { 
      UrlGenerator urlGenerator = new UrlGenerator(); 
      String url = urlGenerator.getDownloadPicture(pack.getImageName()); 
      DownloadPicture downloadPicture = new DownloadPicture(holder.imageView, url, getContext()); 
      downloadPicture.start(); 
     } 
     holder.textView1.setText(pack.getSomething1()); 
     holder.textView2.setText(pack.getSomething2()); 
     holder.textView3.setText(pack.getSomething3()); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView textView1; 
     TextView textView2; 
     TextView textView3; 
     ImageView imageView; 
    } 
} 
+0

我添加了畢加索圖書館並像我一樣更改了我的代碼,並且知道它正常工作,謝謝。 – gfitas