how to pass an array containing urls as an image source to imageadapter of the grid view? I have a working Image adapter. but the problem i开发者_高级运维s the getView method of the adapter returns only one imageview. Can some one tell how to pass an array containing urls to the image adapter of the gridview ? the getview method is here
public View getView(final int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
if (convertView == null) { // if it's not recycled, initialise some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
} else {
imageView = (ImageView) convertView;
}
// Bitmap bmp = loadBitmap(url[i]);
Bitmap bmp = loadBitmap("http://www.fgj.com/aero/planes/boeing/boeingf15.jpg");
System.out.println("in adapter :" + url[i]);
imageView.setImageBitmap(bmp);
//imageView.setImageResource(mThumbIds[position]);
//Bitmap bmp = loadBitmap("http://www.fgj.com/aero/planes/boeing/boeingf15.jpg");
return imageView;
}
ArrayList<String> list;//list of image paths
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
Bitmap myBitmap = BitmapFactory.decodeFile("/sdcard/foldername/"+list.get(position));
imageView.setImageBitmap(myBitmap);
imageView.setBackgroundColor(-1);
return imageView;
}
Unfortunately this is much harder than it needs to be. The overview of what you need to do is:
- Collect the urls you need to fetch.
- Create an AsyncTask to fetch each one in turn or use a pool of AsyncTasks in a thread-pool fashion to service the urls. Somewhere you need to keep track of which url belongs to which position in your
AdapterView
so you can selectively invalidate rows as their images come in. - Cache the images you get back to the SD card or in memory.
- Invalidate rows as their images come in, causing them to reload from the cache.
Personally I think the framework should supply a "WebImage" widget that would make this a bit easier. I guess there are just too many moving parts that it makes sense for each app to implement this on its own.
Edit - unless you use a library and let someone else do the dirty work. Apparently DroidFu has a solution for this problem: ImageLoader. I haven't used it, but it's probably less error prone than rolling one's own.
精彩评论