我有一个包含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
};
}
}
请纠正我。谢谢提前。