1

我目前在我的应用程序中使用(或至少尝试使用)Droidparts ImageFetcher。有可用的示例代码展示了它如何与 CursorAdapter 一起工作,但我无法理解所有这些并用我的 ArrayAdapter 复制它。我我了解 InjectDependency 的工作原理,但如果可以进一步解释或不包含在答案中,我将不胜感激。

无论如何,我的问题是,一旦它们被回收,我如何阻止我的 imageViews 加载错误的图像?我尝试将 ImageHolder 作为标签附加到视图,但它没有用,我看不到它背后的逻辑......

这是我的代码:

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

任何指针或示例将不胜感激,谢谢!

4

1 回答 1

1

我建议您扩展org.droidparts.adapter.widget.ArrayAdapter已经包含LayoutInflater.

如果你这样做,这里是代码,逻辑应该很容易理解:

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(...)方法,作为在构造函数中提供对象列表的替代方法。当然,请确保您已声明<uses-permission android:name="android.permission.INTERNET" />.

于 2014-01-13T11:20:30.600 回答