在ArrayAdapter使用ImageFetcher回收问题 [英] Recycling issues in ArrayAdapter using ImageFetcher

查看:157
本文介绍了在ArrayAdapter使用ImageFetcher回收问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的(或者至少尝试使用)Droidparts ImageFetcher在我的应用程序。
有样品提供code显示它如何与CursorAdapter的,但我一直无法理解这一切和我ArrayAdapter复制。我的认为的我所理解的东西InjectDependency是如何工作的,但如果这既可以进一步解释或不包含在答案,我会很感激。

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.

不管怎么说,我的问题是,如何从错误的图像加载阻止我imageViews一旦被回收?我试图附加一个imageHolder的作为标记的看法,但它没有工作,我无法看到它背后的逻辑...

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

下面是我的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;
}

任何指针或例子是大大AP preciated,谢谢!

Any pointer or example would be greatly appreciated, thanks!

推荐答案

我建议你到延伸 org.droidparts.adapter.widget.ArrayAdapter 已包含实例 LayoutInflater

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

如果您这样做,这里的code,逻辑应该很容易遵循:

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;
    }

}

还检查了它的 setContent(...)方法来替代在构造函数中提供对象​​列表。
当然,确保你已经声明&LT;使用许可权的android:name =android.permission.INTERNET对/&GT;

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" />.

这篇关于在ArrayAdapter使用ImageFetcher回收问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆