0

I have a gallery widget in my Android application, displaying images of cars (it's not really cars, but cars works as an example). The gallery shows one model of car per item in the gallery, so we might have a gallery like:

  1. Ford Model T
  2. Porsche 911
  3. Ford Mustang

When the user long presses on an item in the gallery, they can select a new image for the car in question — so if the user long presses on the Ford Mustang image, they see a new gallery of pictures of Ford Mustangs, and they can pick the image that they actually want to see in the main gallery (maybe they want to see a red one instead of a yellow one).

This is working pretty well in code — I just have an array of ints, where there's one int in the array for each model of car, and each int represents an index for an image.

The user selects a new image and then the log says:

MoreCarImagesGallery: User selected image 3 for car with ID 219 (Ford Mustang)
MainGalleryAdapter: Setting selected image (3) for car at position 2

The adapter getView() then does:

Image img = imageLookup.getImageForCar(car.id, selectedImageIndexes[position]);

However! When I go back to the main gallery, the previously displayed image for the car in question is still showing. But! When I scroll a few items to the left or right away from the displayed car, and then scroll back to that car, the image updates to the "new" (user selected) image.

So obviously the gallery is doing some caching of images from my adapter. I probably want that to keep happening, because I imagine the gallery would be very laggy on slower phones without that caching in place.

How do I invalidate images in my gallery in order to force my adapter getView() to return the "new" image?

George
  • 2,110
  • 5
  • 26
  • 57

1 Answers1

1

Assuming you have actually updated the data behind your gallery adapter can call notifyDatasetChanged() on the adapter of the main gallery after you have changed the data.

http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()

sgarman
  • 6,152
  • 5
  • 40
  • 44
  • Thanks, that works great - is there a way to invalidate just the item in question instead of the whole cache at once? – George Jan 24 '13 at 23:37
  • 1
    You could do something like this: http://stackoverflow.com/a/9987714/317862 but from my testing you really are not going to see much of a performance increase. It's standard to just refresh the list, if someone has a different observation I'd love to see it. – sgarman Jan 24 '13 at 23:40
  • Fair enough, I'll just leave it unless I see a performance decrease somwhere. Probably unlikely like you said. – George Jan 24 '13 at 23:56