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;
    }

adapter2)を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 に答える