public class myPage extends Activity { private Gallery g =null; private Context mContext = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cheongfun); if (g==null) { Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(new ImageAdapter(this)); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(myPage.this, "" + position, Toast.LENGTH_SHORT).show(); } }); g.setSelection(3); } } @Override public void onPause() { super.onPause(); g = null; finish(); } @Override public void onStop() { super.onStop(); g = null; finish(); } public class ImageAdapter extends BaseAdapter { int mGalleryItemBackground; private Context mContext; private Integer[] mImageIds = { R.drawable.cheongfun1, R.drawable.cheongfun2, R.drawable.cheongfun3, R.drawable.cheongfun4, R.drawable.cheongfun5, R.drawable.cheongfun6, R.drawable.cheongfun7 }; public ImageAdapter(Context c) { mContext = c; TypedArray a = obtainStyledAttributes(R.styleable.DefaultGallery); mGalleryItemBackground = a.getResourceId( R.styleable.DefaultGallery_android_galleryItemBackground, 0); a.recycle(); } public int getCount() { return mImageIds.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 i = new ImageView(mContext); i.setImageResource(mImageIds[position]); i.setLayoutParams(new Gallery.LayoutParams(300, 200)); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setBackgroundResource(mGalleryItemBackground); return i; } } public void onResume() { super.onResume(); setContentView(R.layout.cheongfun); if (g==null) { Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(new ImageAdapter(this)); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(myPage.this, "" + position, Toast.LENGTH_SHORT).show(); } }); g.setSelection(3); } } public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentView(R.layout.cheongfun); if (g==null) { Gallery g = (Gallery) findViewById(R.id.gallery); g.setAdapter(new ImageAdapter(this)); g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(myPage.this, "" + position, Toast.LENGTH_SHORT).show(); } }); g.setSelection(3); } }