I have a UI that consists of a Gridview loaded with a custom adapter. A ButtonAdapter, in this case. So the grid loads fine, the button clicks function like I want them to, but now I have to indicate on the button that it the “active” selection.
I thought I’d do this by just keeping track and changing the background. As it turns out, and based on a couple posts here on SO, the buttons don’t actually exist when they are off screen…and even immediately after a scroll. I’ll often get a NullPointerException when trying to change a button background after scrolling.
I’ve tried changing the views in the adapter to RadioButtons and ToggleButtons, but they all offer similar limitations.
The problem seems mostly to do with the getChildAt() that I use on the grid to “unselect” one button, or whatever, when another one is selected.
Is there a workaround for this, or perhaps another suggestion of similar functionality. A vertically scrollable, grid-like format, etc…
Thanks for the help.
EDIT:
Thanks Craigy…I did forget to put a platform on there o.0…i’ll add android.
Have you considered using a Selector? In ignorance of how your buttonAdapter works or what you’re pulling it from, you can set the background drawable of any View to change according to its state using selectors.
If your selector is defined like so (assuming the presence of appropriate drawables, of course):
And then set your GridView’s choice mode to single Choice:
This lets the OS take care of everything for you in terms of remembering which position is selected in the list and then clearing it out for you when you’ve clicked another one