开发者

Grid view image source issue

i am trying to send an array containing strings of url of the image source to the imageadapter of the grid view. But only the last url of the array is being used to set the image in the adapter. some help here would be appreciated.

GridView gridview = (GridView) findViewById(R.id.gridview);
    ImageAdapter imageListAdapter = null;
    for( int inx = 0 ; inx < imageUrl.length ; inx++)
        {
                 imageListAdapter = new ImageAdapter(context,imageUrl[inx],productName[inx] );
                 System.out.println(imageUrl[inx]);
                 System.out.println(productName[inx]);
                 gridview.setAdapter(imageListAdapter);
        }

adapter code :

public class ImageAdapter extends BaseAdapter {
private Context mContext;
static int i = 0;
String url = null;
String name = null;

public ImageAdapter(Context c, String url , String name ) {
    mContext = c;
    this.url = url;
    this.name = name;
}

public int getCount() {
    // return url.length;
    return ++i;
}

public Object getItem(int position) {
    return null;
}

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

// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, ViewGroup parent) {
    ImageView imageView;

    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;
    }
    //imageView.setImageResource(mThumbIds[position]);
    //Bitmap bmp  = loadBitmap("http://www.xvr.com/aero/planes/boeing/boeingf15.jpg");

    Bitmap bmp  = loadBitmap(url);
    System.out.println("in adapter :"+url);
    //imageView.setImageResource(mThumbIds[position]);
    imageView.setImageBitmap(bmp);
    return imageView;
}
public static Bitmap loadBitmap(String url) 
{
    Bitmap bitmap = null;
    InputStream in 开发者_JS百科= null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(),  4 * 1024);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream,  4 * 1024);

        int byte_;
        while ((byte_ = in.read()) != -1)
            out.write(byte_);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e("","Could not load Bitmap from: " + url);
    } finally {
        try{
            in.close();
            out.close();
        }catch( IOException e )
        {
            System.out.println(e);
        }
    }
    return bitmap;
}

}


This is because you are creating new ImageAdapter every time in your for loop and setting it to Gridview so in the last iteration of your loop the last image is set to adapter and finally that adapter is set to gridview.

Note: Your implementation is wrong have look at following example http://mobiforge.com/designing/story/understanding-user-interface-android-part-3-more-views and search for keyword GridView

Pass url and name as string array just once rather than each time creating new object of ImageAdapter something like below, and change your adapter code accordingly.

imageListAdapter = new ImageAdapter(context,imageUrl,productName);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜