0

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;
}

}

1 Answers1

0

By doing currentImage_ = (ImageView) imageAdapter_.getView(position, view, parent);

you are overriding what currentImage_ is.

I guess it is supposed to point to the imageview you have in your layout. Your app is not crashing just because the elements of the gridview are imageviews as well.

currentImage_ should always point to the detail ImageView and you should only change its content by doing

             currentImage_.setImageResource(imageAdapter_.getSelectedImage()[position]);

In short, remove the previous line.

fedepaol
  • 6,834
  • 3
  • 27
  • 34