What you're experiencing has to do with the way Android recycles views in ListView
, GridView
, etc. You mention that your first checkbox is uncheckable, while your others remain working. I think you'll notice that the others only appear to work properly, since you haven't handled the recycling properly.
The reason your line
FrameLayout layout = (FrameLayout)View.inflate(maincon, R.layout.taste_brand, null);
seems to fix the problem is because this now inflates the views again each time they are used. I'll admit, when I started with this, re-inflating the views seemed to be the best solution; it entirely defeats the purpose of recycling, however, and you lose all the performance benefits otherwise gained.
So now to fix your problem:
First, I highly recommend using the ViewHolder
pattern in conjunction with your BaseAdapter
. More information on that can be found here.
Second, you should probably create a boolean array to match all the items in your GridView
, and use it to determine whether or not an item should be clicked. Set the value of the corresponding boolean inside your checkbox listener and use that value inside getView(..)
to check or uncheck that particular box.
An overall better solution might be to use an array (or list) of models inside your adapter class, each of these containing a boolean field accessible through isChecked
and setChecked(boolean)
. Again, you would use this inside your getView(..)
to display the views properly and change the value inside your checkbox OnCheckedChangeListener
.
Hope that helps.