Tuesday, 20 August 2013

Binding an ImageView with a GridView

Binding an ImageView with a GridView

I had followed the tutorial about the GridView. I am trying to bind an
ImageView with a GridView from a layout xml file via an Adapter. I
captured the ImageView from the mainActivity and I tried both ways by
entering the ImageView through the constructor of the ImageAdapter or by
making the ImageView static. Both of them return a Runtime Exception.
//capturing imageView in the mainActivity
public static ImageView IMAGE_VIEW;
IMAGE_VIEW=(ImageView) findViewById(R.id.imageView1);
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
// It should return 16 ImageViews
return 16;
}
..
..
..
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.CENTER_CROP);
imageView.setPadding(20, 20, 4, 4);
} else {
imageView = (ImageView) convertView;
}
imageView=MainActivity.IMAGE_VIEW; //I suppose here is the
problem
// the code underneath works fine for an Image File not for the
ImageView
// imageView.setImageResource(R.drawable.crazy);
return imageView;
}
}
What is the error here? What would be the solution?

No comments:

Post a Comment