自定义listview适配器,setImageBitmap不加载位图
我为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题出在
loadImage
中,因为您的代码看起来没问题。应该发生以下两件事之一:
loadImage
正在工作,行中的文本应该需要很长时间才能显示,因为getView
直到它才会返回>loadImage
确实如此。loadImage
有错误或正在异步工作,则getView
将立即返回并仅设置您的文本,这就是您所描述的行为。将图像异步加载到 ImageView 中比应有的情况要复杂得多。您可以尝试 DroidFu 的 WebImageView 如果你无法让它工作。
I think the problem is in
loadImage
, as your code looks okay.One of two things should be happening:
loadImage
is working, the text in your rows should be taking a long time to appear becausegetView
won't return until itloadImage
does.loadImage
has an error or is doing work asynchronously, thengetView
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.