Wrong image show up in my ListView rows
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.
The images are downloa开发者_如何学Goded from the web.
How should I solve this problem?
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.
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)
.
If you stop using the convertView you get (which you absolutely should), and generate a whole new View to return every time, does it work right? I think the problem is in the way you reuse the views.
I tried using the solution marked here as correct, but it doesn't solve the problem of wrong image during scrolling. I tryied the second one (Zsombor Erdődy-Nagy) and now it's really ok. So thanks Zsombor :-)
Here it is my snippet:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.tweet_list, null);
}
*/
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.tweet_list, null);
final Status status = getItem(position);
if (status != null) {
TextView statusName = (TextView) v.findViewById(R.id.statusName);
TextView statusText = (TextView) v.findViewById(R.id.statusText);
TextView statusWhen = (TextView) v.findViewById(R.id.statusWhen);
TextView statusScreenName = (TextView) v.findViewById(R.id.statusScreenName);
final ImageView statusUserImage = (ImageView) v.findViewById(R.id.statusUserImage);
statusName.setText(status.getUser().getName());
statusScreenName.setText("@" + status.getUser().getScreenName());
statusText.setText(status.getText());
statusWhen.setText(dateTimeFormatter.format(status.getCreatedAt()));
URL url = status.getUser().getProfileImageURL();
String imageCacheKey = url.getPath();
Drawable cachedImage = imageCache.get(imageCacheKey);
if (null != cachedImage) {
statusUserImage.setImageDrawable(cachedImage);
} else {
new DownloadImageTask(statusUserImage, imageCacheKey).execute(url);
}
}
return v;
}
精彩评论