I have a grid view 3x3. Inside this grid I have ImageView
s.
I set up OnItemClickListener
but when I click on the ImageView
only the first one is pressed in a row no matter which column I am pressing. Also I can be pressing outside the ImageView
(empty space) but still first ImageView
on that row is pressed.
XML
<GridView
android:id="@+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
MainActivity
GridView gridview = (GridView) findViewById(R.id.test);
gridview.setAdapter(new HexAdapter(getBaseContext()));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.e("gridView", "hallo" + position);
}
});
and this is my adapter
public class HexAdapter extends BaseAdapter {
private Context mContext;
public HexAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return (Object) mThumbIds[position];
}
public long getItemId(int position) {
return position;
}
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(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.logo, R.drawable.logo,
R.drawable.logo, R.drawable.logo,
R.drawable.logo, R.drawable.logo,
};
}
R.drawable.logo is just a random image a chose.