in main java class:
gridView = (GridView) findViewById(R.id.gridView1); gridView.setAdapter(new ImageAdapter(this)); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent,View v,int position,long id) { System.out.println("====>>"+position); Intent i = new Intent(getApplicationContext(),GidImage.class); // passing array index i.putExtra("id", position); startActivity(i); } });create a new java file with an image view:
Intent i = getIntent(); // Selected image id int position = i.getExtras().getInt("id"); ImageAdapter imageAdapter = new ImageAdapter(this); ImageView imageView =(ImageView) findViewById(R.id.full_image_view); imageView.setImageResource(imageAdapter.mThumbIds[position]);in custom Adapter class:
public class ImageAdapter extends BaseAdapter { private Context context; public Integer[] mThumbIds = { R.drawable.card0, R.drawable.card1, R.drawable.card2, R.drawable.card3, R.drawable.card0, R.drawable.card1, R.drawable.card2, R.drawable.card3, R.drawable.card0, R.drawable.card1, R.drawable.card2, R.drawable.card3 }; public ImageAdapter(Context context) { this.context = context; } @Override public int getCount() { return mThumbIds.length; } @Override public Object getItem(int position) { return mThumbIds[position]; } @Override public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(context); imageView.setImageResource(mThumbIds[position]); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(new GridView.LayoutParams(130, 100)); imageView.setPadding(8, 8, 8, 8); return imageView; } }
Comments
Post a Comment