Android Question Cannot store views in array in GetAllViewsRecursive loop

Thomas.

New Member
Licensed User
Hi all,

1st I'd like to say thank you for this very informative forum! I've read a lot here and found many solutions. But I could'nt find one for this problem. Maybe someone has an idea (hopefully).

I have 5 ImageViews inside a Panel which I originally copied from the UserInteraceButtonToolbox sample. To get later access to each one of these ImageViews I thought I could copy them to an array while I initialized them:
B4X:
Dim btnPage(6) As ImageView
Dim i As Int
Dim h, w, b As Int

pnlToolbox.Width = Activity.Width
w = pnlToolbox.Width / 5
pnlToolbox.Height = w
h = w * .7
b = (w - h) / 2

For Each v As ImageView In pnlToolbox.GetAllViewsRecursive
    v.Height = h
    v.Width = h
    v.Top = b
    v.Left = w * (v.Tag - 1) + b
    btnPage(v.Tag) = v        ' store reference to ImageView in array
    Log("T: " & btnPage(v.Tag).Tag)
Next

For i = 1 To 5
    Log("t: " & btnPage(i).tag)
Next
While executing this code I get this in the debugging window:
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = true **
T: 1
T: 2
T: 3
T: 4
T: 5
To me this looks as if the array contains all 5 ImageViews. But, while executing this in the same sub
B4X:
For i = 1 To 5
    Log("t: " & btnPage(i).tag)
Next
I get
t: 5
t: 5
t: 5
t: 5
t: 5
** Activity (main) Resume **
** Activity (main) Resume **
And after this all 5 references in the array point to the last inserted ImageView.

What am I doing wrong?
 

Attachments

Last edited:

DonManfred

Expert
Licensed User
B4X:
For Each v As ImageView
will not work. You can not limit the result from GetAllViewRecursive

Try this
B4X:
For Each v As View In pnlToolbox.GetAllViewsRecursive
if v is Imageview then
    dim iv as imageview = v
    [...]
end if
Next
 

Thomas.

New Member
Licensed User
Thank you DonManfred for this quick answer. It works!

What I thought is, that if there are only ImageViews on the panel it should work my way.

Have a nice Easter! And thanks again!
 
Top