错误的图像显示在我的ListView行中

Cur*_*ain 7 android listview

我在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;
}
Run Code Online (Sandbox Code Playgroud)

问题是滚动时; 有时会显示正确的图像,但有时向后/向前滚动时,图像会随机显示,并且与行无关.

图像从网上下载.

我该如何解决这个问题?

ben*_*nvd 26

当Android ListView不再需要时,Android会重新使用列表项.因此,您需要确保所有应更改的视图实际上都会更改.

您的问题是,如果您没有找到当前列表项的drawable,则不会清空也不会隐藏ImageView.你应该做thumb.setImageDrawable(null)那种情况,或者thumb.setVisibility(View.GONE).