I have an ImageView
next to a GridView
and what I'm trying to do is set the ImageView
to the picture selected in the GridView
.
When I run my project, All the thumbnails display in the GridView
(photoGrid_) normally, but when I press a thumbnail, the Toast
displays the correct index, but my ImageView
(currentImage_) doesn't display anything. I've tried similar solutions, but I would either get the same result or my app will crash.
MyActivity
public void gridViewArea()
{
photoGrid_ = (GridView) findViewById(R.id.gridview);
photoGrid_.setAdapter(imageAdapter_);
photoGrid_.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(MyActivity.this, "" + position, Toast.LENGTH_SHORT).show();
currentImage_ = (ImageView) imageAdapter_.getView(position, view, parent);
currentImage_.setImageResource(imageAdapter_.getSelectedImage()[position]);
}
});
}
ADAPTER
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return mThumbIds.length;
}
public Object getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return 0;
}
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(0, 0, 0, 0);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds =
{
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 Integer[] getSelectedImage()
{
return mThumbIds;
}
}