在ArrayAdapter的getView()方法中只下载一次图像

时间:2013-12-03 09:54:21

标签: android listview android-arrayadapter

我有一个从网址下载的图片列表(包含说明)。一切都很好,直到我将列表滚动到顶部。再次拍摄照片,因为(我猜)方法再次调用getView()。如何使图像只下载一次?在ArrayAdapter类下面

public class ImageWithTwoTextArrayAdapter extends
ArrayAdapter<ImageWithTwoText> {
    public final static String TITLE_KEY = "title";
    public final static String SUBTITLE_KEY = "subtitle";
    public final static String IMAGE_RESOURCE_KEY = "imageResources";

    private final List<ImageWithTwoText> imageWithTwoTextList;
    private final Context context;

    TextView titleView;
    TextView subTitleView;
    ImageView imageView;

    /**
     * Class constructor
     * 
     * @param context
     */
    public ImageWithTwoTextArrayAdapter(Context context,
        List<ImageWithTwoText> imageWithTwoTextList) {
    super(context, R.layout.row_view, imageWithTwoTextList);
    this.context = context;
    this.imageWithTwoTextList = imageWithTwoTextList;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.row_view, parent, false);

    ImageWithTwoText currentElement = imageWithTwoTextList.get(position);

    titleView = (TextView) rowView.findViewById(R.id.title);
    subTitleView = (TextView) rowView.findViewById(R.id.subtitle);
    imageView = (ImageView) rowView.findViewById(R.id.icon);

    titleView.setText(currentElement.getTitle());
    subTitleView.setText(currentElement.getSubTitle());
    if (currentElement.getImageResource() != null) {
        imageView.setImageResource(currentElement.getImageResource());
    } else {
        if (imageView.getDrawable() == null) {
        new DownloadThumbnailTask(imageView).execute(currentElement
            .getImageURI());
        }
    }

    return rowView;
    }
}

1 个答案:

答案 0 :(得分:0)

使用this教程来缓存称为延迟加载的图像,我正在使用它,它就像魅力一样,只需两个步骤。