Invalid image displayed on my ListView.

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 scrolling; sometimes the correct image is displayed, but sometimes when scrolling back / forward, the images are displayed randomly and are not associated with the line.

Images are downloaded from the Internet.

How do I solve this problem?

+6
android listview
source share
3 answers

Android ListView reuses list items when they are no longer needed. For this reason, you need to make sure that all views that need to be changed are truly changed.

Your problem is that if you do not find what is available for the current list item, you will not empty and hide ImageView . You must do thumb.setImageDrawable(null) in this case or thumb.setVisibility(View.GONE) .

+24
source share

If you stop using the convertView that you get (which you absolutely need) and generate a whole new View to return every time, does it work correctly? I think the problem is how you reuse views.

-one
source share

I tried to use the solution marked here as correct, but it does not solve the problem of the wrong image while scrolling. I tried the second (Zsombor ErdΕ‘dy-Nagy), and now everything is in order. So thanks Zsombor :-)

Here 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; } 
-one
source share

All Articles