Wrong image show up in my ListView rows

后端 未结 3 1786
孤街浪徒
孤街浪徒 2020-12-10 06:46

I use this code in my getView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null)         


        
相关标签:
3条回答
  • 2020-12-10 07:26

    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;
    }
    
    0 讨论(0)
  • 2020-12-10 07:36

    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.

    0 讨论(0)
  • 2020-12-10 07:43

    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).

    0 讨论(0)
提交回复
热议问题