How can I download ListView HTML images asynchronously?

夙愿已清 提交于 2019-12-20 06:58:14

问题


As a prototype, I'm trying to modify the android-imagedownloader demo from the Android Developers Blog to work with HTML image tags rather than hard-coded ImageViews. I've modified it by replacing ImageAdapter.getView() with the following:

public View getView(final int position, View view, final ViewGroup parent) {
   TextView textView = (TextView) view;
    if (textView == null) {
       textView = new TextView(parent.getContext());
       textView.setPadding(6, 6, 6, 6);
    }
    textView.setText(Html.fromHtml("<img src=\"" + URLS[position] + "\"/>", 
       new ImageGetter() {
       @Override public Drawable getDrawable(final String source) {
          ImageView imageView = new ImageView(parent.getContext());
          imageDownloader.download(source, imageView);
          return imageView.getDrawable();
       }
    }, null));

    return textView;
}

None of the images are appearing in the ListView though and no errors are reported in Logcat. Do TextViews unlike ImageViews need to be refreshed somehow once the images are downloaded?

The original getView() was:

public View getView(int position, View view, ViewGroup parent) {
    if (view == null) {
        view = new ImageView(parent.getContext());
        view.setPadding(6, 6, 6, 6);
    }

    imageDownloader.download(URLS[position], (ImageView) view);

    return view;
}

回答1:


I'm going to abandon my original idea of using HTML TextView to render my images since I don't think it will be a problem to limit the layout to a single image per TextView. For those who still really want to do this, this answer should guide you to the right solution. You'd have to pass the TextView to the BitmapDownloaderTask and in onPostExecute call setText again but with the newly downloaded or "decached" image.

It might also be necessary to force an update of the row in the ListView per Romain Guy's guidance.



来源:https://stackoverflow.com/questions/11712130/how-can-i-download-listview-html-images-asynchronously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!