Skip to content Skip to sidebar Skip to footer

How To Change Image In Gridview?

I have a problem with GridView. I want to change image through code in GridView (not OnClickListener etc.) but I have lost any hope...here's my code: ImageAdapter: package com.exam

Solution 1:

You can add a new method to your ImageAdapter, something like this:

publicvoidchangeImage(int position, int image){
    if(position >= 0 && position < images_id.length){
        image_id[position] = image;
        notifyDataSetChanged();
    }
}

And here is how you can use it:

GridViewgrid= (GridView) findViewById(R.id.gridView);
((ImageAdapter)grid.getAdapter()).changeImage(0, R.mipmap.sword_of_faith);

Also, you should consider setting the GridView and ImageAdapter instances as class variables so you won't have to create them every time you need to use them.

Post a Comment for "How To Change Image In Gridview?"