我想在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
謝謝你提前。 對不起,我的英語不好。
問題是由於意見回收。您可以使用「畢加索」或任何其他圖像加載庫來避免此問題。 –