0

我有一个包含imageview和gridview的活动。规则是当我在gridview上单击选定的图像时,它将由imageview预览。然后,可以单击imageview并将其显示在对话框中。问题是当我单击imageview,该图像与 gridview.here 我的代码中的选定图像不匹配。

public class GalleryActivity extends Activity {

ImageView imgviewThumb, imgviewPreviewDialog;
public int[] imageResto = {
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4,
        R.drawable.img5,
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4
};
int position ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gallery);

    imgviewThumb = (ImageView)findViewById(R.id.imgview_preview);
    imgviewThumb.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Dialog dialog = new Dialog(GalleryActivity.this);
            dialog.setCancelable(true);
            dialog.setContentView(R.layout.dialog_previewimage);
            imgviewPreviewDialog = (ImageView) dialog.findViewById(R.id.imgview_preview_dialog);
            imgviewPreviewDialog.setImageResource(imageResto[position]);
            dialog.show();
        }
    });

    GridView gView = (GridView) findViewById(R.id.grid_View);

    //Instantiating ImageAdapter class
    gView.setAdapter(new ImageAdapter(this));

    /*showing full image on clicking the thumbnail
     * create a new class named FullImage.java 
     */
    gView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            imgviewThumb.setImageResource(imageResto[position]);
        }
    });
}

public class ImageAdapter extends BaseAdapter {
    public ImageAdapter(Context c) {
        mContext = c;
    }

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

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
            imageView.setAdjustViewBounds(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);

        return imageView;
    }

    private Context mContext;

    // images in array
    public Integer[] mThumbIds = {
             R.drawable.thumb1_e,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4,
             R.drawable.thumb5,
             R.drawable.thumb1,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4
    };
}
}

请纠正我。谢谢提前。

4

2 回答 2

1

尝试改变:

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

对此:

public Object getItem(int position) {
        return mThumbIds[position];
}
于 2012-09-11T08:04:42.507 回答
0

我认为你使用了两个不同的数组,所以它可能不匹配,

public int[] imageResto = {
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4,
        R.drawable.img5,
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4
};

  // images in array
    public Integer[] mThumbIds = {
             R.drawable.thumb1_e,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4,
             R.drawable.thumb5,
             R.drawable.thumb1,
             R.drawable.thumb2,
             R.drawable.thumb3,
             R.drawable.thumb4
    };

并在此处放置有效的图像数组位置,使其正确为 0 到数组的长度,因此其写入位置位于此处

 gView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            imgviewThumb.setImageResource(validImageArray[position]);
        }
    });

或者你可以在 getView 中做这样的改变

 public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
            imageView.setAdjustViewBounds(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(imageResto[position]);

        return imageView;
    }
于 2012-09-11T11:17:41.057 回答