Updating images dynamically from URL with Android
So I have this program that grabs the RSS feed at http://www.muttville.org/mutts/feed and displays a list of the available dogs, basically filling out each item in a ListView with an image of the dog on the left, and the headline about the dog on the right.
My application executes and grabs and parses the XML feed fine. onCreate, it stores the image URL as a string in each item. I have an R开发者_如何学编程SSFeed data structure, which is basically an array of RSSItem objects. The problem is figuring out how to update the images asynchronously.
I have an imageDownloader that I actually got from the Android dev blog at Google that updates the images and stores them, but the preload is very long. I'd rather begin displaying the items immediately and updating the images as I get them. Annoyingly, the listview scrolls really unresponsively at first as well, calling my imageDownloader to retrieve the items again. Is there some way I can make the ImageViews just grab a pointer to an ImageView object in the RSSItem, and then just update that?
I'm pretty new to Android development, so there's a good chance I don't realize something basic. Here's my adapter code:
private class FeedItemAdapter extends ArrayAdapter {
private List<RSSItem> items;
public FeedItemAdapter(Context context, int textViewResourceId, List<RSSItem> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.displayitem, null);
}
RSSItem o = items.get(position);
if (o != null) {
ImageView tt = (ImageView) v.findViewById(R.id.leftimage);
TextView bt = (TextView) v.findViewById(R.id.righttext);
if (tt != null) {
//get image from url string
imageDownloader.download(o.getImageString(), tt);
}
if(bt != null){
bt.setText(feed.getItem(position).getTitle());
}
}
return v;
}
}
Create a new class that inherits ImageView, show a 'pending' image by default, and have a function that begins loading from the network in another thread. When it's complete, update the image.
精彩评论