I'm trying to put an imageButton and ImageView on the grid view items, so here is my code shown below, what am trying to do is create an Adapter extending BaseAdapter and then creating a Viewholder and putting both the ImageView and Imagebutton in the holder. However, there are no errors but again when the grid view is opened, the App stops.
Any clue will be of much great help...Thanks!!
public class DestinationActivity extends Activity implements OnClickListener, OnItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view);
GridView grid = (GridView) findViewById(R.id.gvGrid);
myImageAdapter = new ImageAdapter(this);
grid.setAdapter(myImageAdapter);
}
class ViewHolder {
int id;
ImageView imageview;
ImageButton button;
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path) {
itemList.add(path);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.image_button, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.button = (ImageButton) convertView.findViewById(R.id.ibClose);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.button.setId(position);
holder.imageview.setId(position);
holder.imageview.setPadding(1, 1, 1, 1);
holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageview.setLayoutParams(new GridView.LayoutParams(330, 330));
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position),
330, 330);
holder.imageview.setImageBitmap(bm);
holder.button.setOnClickListener(new ImageButton.OnClickListener() {
@Override
public void onClick(View v) {
// // TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hello..!!", Toast.LENGTH_SHORT).show();
}
});
holder.imageview.setImageBitmap(null);
holder.id = position;
return convertView ;
}
}
image_button.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/thumbImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<ImageButton
android:id="@+id/ibClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/close_dialog" />
</RelativeLayout>
grid_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView
android:id="@+id/gvGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:clipToPadding="false"
android:columnWidth="10dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:scrollbarStyle="outsideOverlay"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
/>
</LinearLayout>