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 impa开发者_Python百科ct on the view I return but the image does not appear (and I am sure the bitmap is here)
Thank you
I think the problem is in loadImage
, as your code looks okay.
One of two things should be happening:
- If
loadImage
is working, the text in your rows should be taking a long time to appear becausegetView
won't return until itloadImage
does. - If
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.
精彩评论