自定义listview适配器,setImageBitmap不加载位图

发布于 2024-10-29 23:00:37 字数 1184 浏览 1 评论 0原文

我为 Item 的 listView(myRSSTitleListAdapter) 扩展 ArrayAdapter 创建了自己的适配器(Item 是我的实体类)。我像这样重写了 getView 方法:

public View getView(int position, View converView, ViewGroup parent) {
  View view = converView;
  if(view == null) {
    LayoutInflater li =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    view = li.inflate(layout, null);
  }

  final Item listItem = items[position];

  if(listItem != null) {
    ImageView image = (ImageView)view.findViewById(R.id.image);
    image.setImageBitmap(ImageTool.loadImage(listItem.getImageURL()));

    HTMLUtilities htmlTool = HTMLUtilities.getInstance();
    TextView title = (TextView)view.findViewById(R.id.label);
    title.setText(htmlTool.escapeHTML(listItem.getTitle()));
  }

  return view;
}

ImageTool 是我编写的一个类,loadImage 从传递给它的 url 返回一个位图。 我在 ListActivity 中调用 myRSSTitleListAdapter,如下所示:

setListAdapter(new myRSSTitleListAdapter(this, R.layout.rss_items, items));

items 是一个 Item 数组。

rss_items 是一个带有 imageView 和 textView 的线性布局。

对于textView,一切都很好,setText 对我返回的视图有影响,但图像没有出现(我确信位图在这里)

谢谢

I created my own adapter for a listView(myRSSTitleListAdapter) extended ArrayAdapter of Item (Item is my entity class) . I overrode the getView method like this :

public View getView(int position, View converView, ViewGroup parent) {
  View view = converView;
  if(view == null) {
    LayoutInflater li =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    view = li.inflate(layout, null);
  }

  final Item listItem = items[position];

  if(listItem != null) {
    ImageView image = (ImageView)view.findViewById(R.id.image);
    image.setImageBitmap(ImageTool.loadImage(listItem.getImageURL()));

    HTMLUtilities htmlTool = HTMLUtilities.getInstance();
    TextView title = (TextView)view.findViewById(R.id.label);
    title.setText(htmlTool.escapeHTML(listItem.getTitle()));
  }

  return view;
}

ImageTool is a class I wrote and loadImage returns a bitmap from the url passed to it.
I call myRSSTitleListAdapter in my ListActivity like this :

setListAdapter(new myRSSTitleListAdapter(this, R.layout.rss_items, items));

items is an array of Item.

rss_items is a linearlayout with an imageView and a textView.

For the textView everything is fine and the the setText has an impact on the view I return but the image does not appear (and I am sure the bitmap is here)

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你爱我像她 2024-11-05 23:00:37

我认为问题出在 loadImage 中,因为您的代码看起来没问题。

应该发生以下两件事之一:

  1. 如果 loadImage 正在工作,行中的文本应该需要很长时间才能显示,因为 getView 直到它 才会返回>loadImage 确实如此。
  2. 如果 loadImage 有错误或正在异步工作,则 getView 将立即返回并仅设置您的文本,这就是您所描述的行为。

将图像异步加载到 ImageView 中比应有的情况要复杂得多。您可以尝试 DroidFuWebImageView 如果你无法让它工作。

I think the problem is in loadImage, as your code looks okay.

One of two things should be happening:

  1. If loadImage is working, the text in your rows should be taking a long time to appear because getView won't return until it loadImage does.
  2. If loadImage has an error or is doing work asynchronously, then getView will return instantly and only set your text, which is the behavior you're describing.

Loading images asynchronously into an ImageView is much trickier than it should be. You might try DroidFu's WebImageView if you can't get it to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文