我在 aImageView
旁边有一个GridView
,我想要做的是将 设置ImageView
为在GridView
.
当我运行我的项目时,所有缩略图都正常显示在GridView
(photoGrid_)中,但是当我按下缩略图时,Toast
显示正确的索引,但我的ImageView
(currentImage_)不显示任何内容。我尝试过类似的解决方案,但我要么得到相同的结果,要么我的应用程序将崩溃。
我的活动
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]);
}
});
}
适配器
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;
}
}