我的ListView行中显示了错误的图像 [英] Wrong image show up in my ListView rows
问题描述
我在getView中使用以下代码:
I use this code in my getView:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listrow, null);
}
Order o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
ImageView thumb = (ImageView) v.findViewById(R.id.icon);
if (o.getOrderDrawable() != null) {
thumb.setImageDrawable(o.getOrderDrawable());
} else {
tt.setText(o.getOrderTitle());
}
}
return v;
}
问题出在滚动时;有时显示正确的图像,但是有时在向后/向前滚动时,图像随机显示,并且与该行无关.
The problem is when scrolling; sometimes the correct image shows, but sometimes when scrolling back/forward, the images shows randomly and that is not associated with the row.
图像是从网上下载的.
我应该如何解决这个问题?
How should I solve this problem?
推荐答案
Android的ListView
在不再需要列表项时会重用它们.因此,您需要确保所有应该更改的视图都将被更改.
Android's ListView
reuses list items when they aren't need anymore. For this reason, you need to make sure all views that should change, will actually get changed.
您的问题是,如果找不到当前列表项的可绘制对象,则不会清空也不隐藏ImageView
.在这种情况下,您应该执行thumb.setImageDrawable(null)
或thumb.setVisibility(View.GONE)
.
Your problem is that if you don't find a drawable for the current list item, you don't empty nor hide the ImageView
. You should do thumb.setImageDrawable(null)
in that case, or thumb.setVisibility(View.GONE)
.
这篇关于我的ListView行中显示了错误的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!