0

我有不同的可绘制整数数组,如下所示:

  Integer[] imagesR = {
                    R.drawable.albacoretuna, R.drawable.almonds
            };


 Integer[] imagesACE = {
                    R.drawable.captopril, R.drawable.lisinopril, R.drawable.vasotec
            };

每个整数数组应该只显示基于用户点击的。

以下是我尝试过的:

  Gallery gallery = (Gallery) findViewById (R.id.gallery3);

    Bundle extras = getIntent().getExtras();
       if (extras != null) {
            String value = extras.getString("medication");
            if( value.equals("Angiotensin II Receptor Blockers") ){
                header.setText("Angiotensin II Receptor Blockers");
                note.setText(R.string.receptorblockers);
                gallery.setAdapter(new ImageAdapter.IMAGE_SET_ONE);

            }
            else if( value.equals("Angiotensin Converting Enzyme (ACE) Inhibitors") ){
                header.setText("Angiotensin Converting Enzyme (ACE) Inhibitors");
                note.setText(R.string.aceinhibitorsdescription);


            }



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

    Integer[] mImageIds = {

    };

    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 static final int IMAGE_SET_ONE = 1;
    public static final int IMAGE_SET_TWO = 2;

    private int mImageSet;

    public void setImageSet(int imageSet) {
        mImageSet = imageSet;
    }

    public View getView(int pos, View convertView, ViewGroup arg2) {
        ImageView i;
        //= new ImageView(mContext);

        if (convertView == null) {  // if it's not recycled, initialize some attributes
            i = new ImageView(mContext);
            i.setLayoutParams(new GridView.LayoutParams(85, 85));
            i.setScaleType(ImageView.ScaleType.CENTER_CROP);
            i.setPadding(8, 8, 8, 8);
        } else {
            i = (ImageView) convertView;
        }

        if(mImageSet == IMAGE_SET_ONE) {
            // load the correct image in...
            Integer[] imagesACE = {
                    R.drawable.captopril, R.drawable.lisinopril, R.drawable.vasotec
            };
            i.setImageResource(imagesACE[pos]);
        } else if(mImageSet == IMAGE_SET_TWO) {
            // load the correct image in...
            Integer[] imagesR = {
                    R.drawable.albacoretuna, R.drawable.almonds
            };
            i.setImageResource(imagesR[pos]);
        }

        //i.setImageResource(mImageIds[arg0]);
        return i;
    }
}

但我似乎无法弄清楚如何正确地做到这一点。有没有人已经尝试过这样的事情?你能帮我完成这个吗?我会很感激你的帮助。谢谢。

4

1 回答 1

0

1)改变你的构造函数ImageAdapter是这样的:

public ImageAdapter(Context c,int imageSet) {
        this.mContext = c;
        this.mImageSet=imageSet;
    }

2)当您尝试将 设置adapter为您的Gallery实例时,请使用以下代码:

//case to display drawables in array1
gallery.setAdapter(new ImageAdapter(YourActivity.this, ImageAdapter.IMAGE_SET_ONE));

//case to display drawables in array2
gallery.setAdapter(new ImageAdapter(YourActivity.this, ImageAdapter.IMAGE_SET_TWO));
于 2013-03-11T16:56:27.807 回答