Android Question I dynamically created listviews and broke evrything

mrred128

Active Member
Licensed User
Longtime User
It seamed like a good thing at the time......

I have an app that needs to walk through many pics. I have a panel that I create a 3x3 matrix of list views and matching check boxes. Working with an array of objects simplifies a major amount of coding. As far as I know, there is now way of adding or defining an array in the designer script. So I moved this function to the program.

I'm guessing that the auto scaling is the beast that is missing. That is, it looked pretty when it was hard coded in the designer script.

B4X:
...
    Private IV(9) As ImageView
    Private CB(9) As CheckBox
...
Sub SetupView
    ' set the width of the objects
    Dim width As Int = (PAN1.width / 3)
    Dim height As Int = (PAN1.height / 3)
    ' set the spacing of the objects
    Dim spacew As Int = (width / 5)
    Dim spaceh As Int = (height / 5)
    ' adjust the views for the spacing
    width = width - spacew
    height = height - spaceh
    Dim a As Int
    Dim r As Int = 0
    Dim c As Int = 0
    For a=0 To 8
        IV(a).Initialize("")
        CB(a).Initialize("")
        PAN1.AddView(IV(a),(r * height) + (spaceh * r),(c * width) + (spacew * c),width,height)
        PAN1.AddView(CB(a),(r * height) + (spaceh * r),(c * width) + (spacew * c),width,height)        
        c = c + 1
        If c = 3 Then
            c = 0
            r = r + 1
        End If
    Next
End Sub

This code is executed on every resume. The check box is supposed to be above the the imageview and all is function, just not pretty. It looks equally messed up in portrait or landscape.
 

mrred128

Active Member
Licensed User
Longtime User
For anyone interested.... my fixed coding is as follows.....

B4X:
    Dim width As Int = (PAN1.width / 3)
    Dim height As Int = (PAN1.height / 3)
    Dim a As Int
    Dim r As Int = 0
    Dim c As Int = 0
    For a=0 To MaxPics - 1
        If IV(a).IsInitialized = False Then
            ' if the object is NOT intalized, we also need to add it to the view
            IV(a).Initialize("")
            PAN1.AddView(IV(a),0,0,0,0) ' no values here, as we need to scale the object first
            Scale.ScaleViewDS(IV(a))
        End If
        IV(a).Top = r * (height)
        IV(a).Left = c * (width)
        IV(a).width = width
        IV(a).height = height
        If CB(a).IsInitialized = False Then
            ' if the object is NOT intalized, we also need to add it to the view
            CB(a).Initialize("")
            PAN1.AddView(CB(a),0,0,0,0) ' no values here, as we need to scale the object first
            Scale.ScaleViewDS(CB(a))
        End If
        CB(a).Top = r * (height)
        CB(a).Left = c * (width)
        CB(a).width = width
        CB(a).height = height
        c = c + 1
        If c = 3 Then
            c = 0
            r = r + 1
        End If
    Next

MaxPics is 9, for an array of 0-8 elements for a 3x3 display grid. This has to be executed on activity resume.

And it only makes sense that it has broken something else...... but that would be another post...:)
 
Upvote 0
Top