Android Question unwanted auto increment of a variable

DALB

Active Member
Licensed User
Hello everyne,

Happy new year and keep healthy for a long time

This morning, I'm faced to a little problem I don't understand.
Maybe I'm not really well awaked, but here is it:

In the sub below, at line 8, I count the number of sub panels (pan(x)) in a parent panel (pnlpnl).
The process runs, but at line 15, there is an auto increment which gives me a bad value.
Normally, each time I create a sub panel, it is placed in the parent panel and the number of the sub panels must increment by a step of 1 unit.
So, I would have : 0,1, 2, 3, 4, and so on

But, in line 17, there is an autoincrement which gives me this :
0,2,4,6,8, and so on; It creates a gap between the sub panels when they takes their place in the parent panel.

So, where am I wrong ? Where does it come from ?

B4X:
[CODE=b4x]Sub ajoutEtiquetteSousPanel
    Dim n As Int=0
    'mise en place des ajouts des panels unit
    'creation et numerotation du panel unité
    For Each pp As View In pnlpnl.GetAllViewsRecursive
        If pp Is Panel Then n=n+1
    Next
    Log("np1 " & n)
    Dim np1 As Int=n+1'le nombre d'units / number of units
    Dim npa As Int=np1-1'le numero attribué / number of the current unit
    Dim pan(np1) As Panel
    pan(npa).Initialize("pUnit")
    pan(npa).Tag="sp" & np
    pan(npa).LoadLayout("Unit")
    Log("np2 " & np1)
    Dim posy As Int=pnlpnl.Top+5dip+(pnlUnit.Height+5dip)*(np1-1)
    pnlpnl.AddView(pan(npa),0,posy,302dip,122dip)
    lblId.Text="sp-" & (npa)
[/CODE]
 

DALB

Active Member
Licensed User
Hello Klaus !

Thank for answering.
Yes, There is a panel in the unit layout

When you write

"this could be the problem, because of pnlpnl.GetAllViewsRecursive which counts also the subpanel. "

does it mean that the subpanel 'unit' I have is counted twice or is there something I have missed ?

when pnlpnl.GetAllViewsRecursive runs, I presume it only counts the panels inside it. So, why the increment doubles ?
 
Upvote 0

DALB

Active Member
Licensed User
Hmmmm !
I understand that when I create a panel by

B4X:
Dim pan(np1) As Panel

in which there is the unit panel, it makes 2 panels ! Yes

B4X:
pan(npa).LoadLayout("Unit")

renders the second panel
But, I cant place them one after the other, there always is a gap of a subpanel.height between 2 subpanels
I'am trying how to solve it !
 
Upvote 0

DALB

Active Member
Licensed User
...found the solution:

B4X:
Sub ajoutEtiquetteSousPanel
    
    Dim n As Int=0
    'mise en place des ajouts des panels unit
    'creation et numerotation du panel unité
    For Each pp As View In pnlpnl.GetAllViewsRecursive
        If pp Is Panel Then n=n+1
    Next
    Log("np " & n)
    Dim np1 As Int=n+1'le nombre d'units / number of units
    Dim npa As Int=np1-1'le numero attribué / number of the current unit
    Dim pan(np1) As Panel
    pan(npa).Initialize("pUnit")
    pan(npa).Tag="sp" & np
    pan(npa).LoadLayout("Unit")
    Log("np2 " & np1)
    Dim posy As Int=pnlpnl.Top+5dip+(pnlUnit.Height+5dip)*(n/2)'(npa-1)
    'Dim posy As Int=pnlpnl.Top+5dip+(122dip+5dip)*(npa-1)
    pnlpnl.AddView(pan(npa),0,posy,302dip,122dip)
    lblId.Text="sp-" & (npa)

Each time I place a new unit panel, in fact, I place 2 panels:
'pan(x)' is the first
'unit' which is add in pan(x) is the second.

Today is the 6th of january...I think having a rest for this year !
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As pnlpnl is the parent you are adding the created panels to you could try:

B4X:
    For Each pp As View In pnlpnl.GetAllViewsRecursive
        If pp Is Panel And pp.Parent = pnlpnl Then n=n+1
    Next

edit: I was too slow, but an alternative
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
when pnlpnl.GetAllViewsRecursive runs, I presume it only counts the panels inside it. So, why the increment doubles ?
No, it counts all Panels.
If there is a Panel in pnlpnl, GetAllViewsRecursive looks also for all Views in this Panel.

If you want only the Views in pnlpnl, you could use a code similar to this:
B4X:
n = 0
For i = 0 To pnlMain.NumberOfViews - 1
    Private v As View
    v = pnlMain.GetView(i)
    If v Is Panel Then
        n = n + 1
    End If 
Next

And if you have only Panels in pnlpnl you could use this:
B4X:
n = pnlMain.NumberOfViews
 
Last edited:
Upvote 0

DALB

Active Member
Licensed User
Thank you you two, it helps me a lot to understand what is counted !
 
Upvote 0
Top