Android自定义Gallery Adapter问题

时间:2011-07-04 07:35:28

标签: android gallery

我在同一个屏幕上有三个画廊。现在我已将Adapter类设置为具有不同资源的这些库。但是我所有的画廊都充满了我为第三个画廊提供的最后一组输入资源。这是我的代码,

    gallery_one.setAdapter(new ImageAdapter(this,Config.Jewel_Images));
    gallery_two.setAdapter(new ImageAdapter(this,Config.designs_Images));
    gallery_three.setAdapter(new ImageAdapter(this,Config.recommended_Images));

public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
        // mGalleryItemBackground = a.getResourceId(
        // R.styleable.HelloGallery_android_galleryItemBackground, 0);
        a.recycle();

    }
    public ImageAdapter(Context c,Integer gallery[]) {
        mContext = c;
        mImageIds=gallery;
    }
    public int getCount() {
        return mImageIds.length;
    }

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

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

    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView i = new ImageView(mContext);
        pos = arg0;
        i.setImageResource(mImageIds[arg0]);
        i.setLayoutParams(new Gallery.LayoutParams((screen_width/3),(screen_height/6)));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        // i.setBackgroundResource(mGalleryItemBackground);
        return i;
    }
}

我不知道问题是什么,我的所有画廊都填充了传递给适配器类的最后一个值。

任何人都可以帮助我吗?。

2 个答案:

答案 0 :(得分:1)

 mImageIds=gallery;

在CustomAdapter中定义mImageIds ...我猜是因为mImageIds是全局声明的,所以每次设置适配器时都会覆盖它的值。

答案 1 :(得分:0)

试试这个适配器:

public class ImageAdapter extends BaseAdapter {

    private Context ctx;
    int imageBackground;

    public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }

    public int getCount() {

        return pics.length;
    }

    public Object getItem(int arg0) {

        return arg0;
    }

    public long getItemId(int arg0) {

        return arg0;
    }

    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(ctx);
        iv.setImageResource(pics[arg0]);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150,120));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }

}