First time poster!
I am new to Android development and have been following Google\'s HelloView tutorials without problems... until the HelloGridView tutorial. For some r
we can use this code,
public class MyGridView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animalsgridview);
GridView gridview = (GridView) findViewById(R.id.gridviewid);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View v, int position, long id) {
int imageId = ((ImageAdapter)parent.getAdapter()).mygetItemId(position);
Intent fullScreenIntent = new Intent(v.getContext(),Full.class);
fullScreenIntent.putExtra(Full.class.getName(),imageId);
AnimalsGridView.this.startActivity(fullScreenIntent);
}
});
}
private class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return imagearray.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return imagearray[position];
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(1, 1, 1, 1);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imagearray[position]);
return imageView;
}
// references to our images
private Integer[] imagearray = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
public int mygetItemId(int position) {
return imagearray[position];
}
}
}
this is the xml code