0

我想創建一個GridView並從URL加載一些圖像到這個GridView,但在加載圖像時有一個問題,但我找不到它,任何人都可以幫忙嗎?GridView中的URL圖像

package com.images; 
import java.io.InputStream; 
import java.net.URL; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 

import com.coboltforge.slidemenuexample.MainActivity; 
import com.coboltforge.slidemenuexample.R; 

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    // Keep all Images in array 
    public Integer[] mThumbIds = new Integer [27]; 

    // Constructor 
    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    @Override 
    public int getCount() { 
     return mThumbIds.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return mThumbIds[position]; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     /* 
     * ImageView imageView = new ImageView(mContext); 
     * imageView.setImageResource(mThumbIds[position]); 
     * imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     * imageView.setLayoutParams(new GridView.LayoutParams(130, 130)); 
     * return imageView; 
     */ 
     ImageView imageView; 
     if (convertView == null) { 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(150, 150)); 
      imageView.setAdjustViewBounds(false); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setPadding(8, 8, 8, 8); 
     } else { 
      imageView = (ImageView) convertView; 
     } 
     for (int i = 0; i < MainActivity.presentersImages.size(); i++) { 
      imageView 
        .setImageDrawable(LoadImageFromWebOperations(MainActivity.presentersImages 
          .get(i) + position)); 

     } 
     return imageView; 
    } 

    protected Drawable LoadImageFromWebOperations(String url) { 
     try { 
      InputStream is = (InputStream) new URL(url).getContent(); 
      Drawable d = Drawable.createFromStream(is, "src name"); 
      return d; 
     } catch (Exception e) { 
      System.exit(0); 
      return null; 
     } 
    } 
} 

MainActivity.presentersImages是一個字符串的ArrayList包含圖像

+0

看看HTTP: //stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview – 2012-08-07 13:16:03

回答