It looks like strange behavior, but is not a bug. In short, don't rely
on the order in which the getView is called with respect to the
method's 'position' parameter. The GridView, like the ListView is
trying to re-use as many of the Views (convertView value) as possible
for resource conservation.

To tie a GridView-item's View (=convertView) to an element in the
adapter, use the GridView-item's setTag method (assign the id of the
Adapter's element on position 'position' by calling setTag on the View
that getView returns).

Then to get the visible items in the GridView later, outside of the
Adapter, you get the GridView's child-views (note; if your adapter has
100 elements, but only 10 are visible in your GridView, GridView will
never have more than 10 child-views). Then iterate of these child-
views and call 'getTag' on them to figure out which Adapter-element is
tied to each of them.



On Sep 22, 4:30 pm, k_pip_k <[email protected]> wrote:
> Hello,
>
> This is a two part post.  First part, if it is a bug, where do I
> report bugs?  Second part, is the behavior I'm observing.
>
> 1.  GridView has method to set its internal Adapter.
> 2.  Derive a new adapter from BaseAdapter, override method getView
> (int,View,ViewGroup)
> 3.  In getView method, put code to instantiate a View grid element
> that is displayed on the grid.
> Use the samples that are part of the android SDK, ie
>
> if (convertView == null) {
>     // create the view} else {
>
>     // cast the view element
>
> }
>
> return the View;
>
> 4.  Create a local array in the Adapter so you can save a pointer to
> each one of the elements you created in the getView method mentioned
> above for later use.
> 5.  Notice through a debugger, GridView goes through 1 -> N, (+1)
> elements during initialization.
> For example, if I have 5 elements in the array of the Adapter, the
> getView method will be called as such:
> getView(0,...) where 0 is the position, null 2nd arg
> getview(1,...)
> getview(2,...)
> getview(3,...)
> getview(4,...)
> getview(0,...)    <-- one too many.  Doesn't maintain pointer to this
> View, 2nd arg still null
>
> This would be ok, but GridView still maintains a pointer to the
> original element (View) created the very first time for position
> Zero.  If I don't handle this correctly, I lose the pointer to the
> first element, and so I can't manipulate the View.  So I have a
> special case to handle the fact I get an extra call with position 0
> coming in and the second element being null.
>
> I don't think this is normal behavior and it looks like GridView's
> initialization loop is flawed.  I don't know, please help me
> understand if I'm wrong.  In the meantime, I'll try to dig around the
> source.
>
> thanks in advance,
>
> K
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to