Recycling issues in ArrayAdapter using ImageFetcher

I'm currently using (or at least attempting to use) Droidparts ImageFetcher in my application. There is sample code available showing how it works with CursorAdapter but I haven't been able to understand all of it and replicate it with my ArrayAdapter. I think I understand how the InjectDependency stuff works, but if that could either be further explained or not included in the answer, I'd be grateful.

Anyways, my question is, how do I stop my imageViews from loading with the wrong images once they have been recycled? I've tried attaching an ImageHolder as a tag to the view but it hasn't worked and I can't see the logic behind it...

Here's my code:

@Override
public View getView( int position, View view, ViewGroup parent )
{
    if( view == null )
    {
        LayoutInflater inflater = ( LayoutInflater ) m_context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        view = inflater.inflate( m_resourceId, parent, false );
    }

    TextView titleView = ( TextView ) view.findViewById( R.id.title );
    if( titleView != null ) titleView.setText( m_values.get( position ).title );

    ImageView imageView = ( ImageView ) view.findViewById( R.id.image );
    if( imageView != null )
    {
        imageView.setImageDrawable( null );

        ViewHolder holder = new ViewHolder();
        holder.imageView = imageView;

        m_imageFetcher = new ImageFetcher( m_context );
        m_imageFetcher.attachImage(m_values.get( position ).imageUrl, holder.imageView, null, 10, null);

        view.setTag(holder);
    }

    return view;
}

Any pointer or example would be greatly appreciated, thanks!


I'd recommend you to extends org.droidparts.adapter.widget.ArrayAdapter that already contains an instance of LayoutInflater .

If you do so, here's the code, the logic should be easy to follow:

public class MyArrayAdapter extends ArrayAdapter<MyModel> {

    private final ImageFetcher imageFetcher;

    public MyArrayAdapter(Context ctx, List<MyModel> list) {
        super(ctx, list);
        imageFetcher = new ImageFetcher(ctx);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null) {
            view = layoutInflater.inflate(m_resourceId, null);
            ViewHolder holder = new ViewHolder();
            holder.titleView = ViewUtils.findViewById(view, R.id.title);
            holder.iconView = ViewUtils.findViewById(view, R.id.image);
            view.setTag(holder);
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.iconView.setImageDrawable(null);
        MyModel item = getItem(position);
        holder.titleView.setText(item.title);
        imageFetcher.attachImage(item.iconUrl, holder.iconView);
        return view;
    }

    private static class ViewHolder {
        TextView titleView;
        ImageView iconView;
    }

}

Also check out it's setContent(...) method as an alternative to providing object list in the constructor. And of course make sure that you've declared <uses-permission android:name="android.permission.INTERNET" /> .

链接地址: http://www.djcxy.com/p/93112.html

上一篇: 如何在Android中的ListView中显示图像列表?

下一篇: 使用ImageFetcher回收ArrayAdapter中的问题