Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

android - 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 downloaded from the web.

How should I solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...