I am new to Android and Java. I am building an app that allows users to push a button which launches an image chooser where they can select from images on the sd card. The app loads with a grid view with 2 cells. One cell has an image view that has a default image. The other is the button. Once, the image is chosen, the image view needs to be displayed in the Image View of the Grid View.
I am using a string path that is being decoded from the images uri to create a bitmap. I then am calling imageView.setImageBitmap(bitmap). This is doing nothing. I have tried updating the image resource with a different image in the drawable folders and still nothing. I added a seperate image view just under the grid in my activity, and that image is updating fine which leads me to believe that this has to do with the grid view (this is my first grid view).
Any help is very much appreciated.
Thank you.
GridViewis maybe not the right choice if you’ve only got two cells. You will probably have an easier time with a simpleLinearLayoutthat has the two items you’re dealing with.But, here’s how you do it with a
GridView:Create your adapter by extending
BaseAdapter. The most important method isgetView(), but it’s where you should be doing the least amount of work.When the
GridViewgoes to redraw itself, it’ll go through for each visible position and callgetView()for each one. You’ll be supplied aView. If the view isnull, inflate a new one from your XML resources, populate it, and return it. If the view is NOTnull, populate the existing one with the appropriate data. The widget is being efficient by recycling views.The trick you need is that when you want the image to change, you need to call
notifyDataSetChanged()on your adapter in order to trigger the redraw (which is when you’ll populate the new image in the view for the appropriate cell).That’s the two-minute version. At Google I/O a year or two back, they did a talk on The World of
ListViewand posted it on YouTube. It was a good talk and pretty much everything there applies toGridViewas well.