开发者

How to pass an array containing urls to an image source to imageadapter of the grid view?

开发者 https://www.devze.com 2023-02-17 07:44 出处:网络
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 on

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:

  1. Collect the urls you need to fetch.
  2. 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.
  3. Cache the images you get back to the SD card or in memory.
  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消