Android Question Calculate number of rows to display images

tsteward

Well-Known Member
Licensed User
Longtime User
Logic fails me :(
If I have a custom list view showing 4 images per row and say 87 images how would I calculate the number of rows and add the images without breaking code.
I'm sure its obvious but I can't figure it out. I either end up one row short or a row with only one image but crash because im trying to add four images.

here is what I was trying
B4X:
Dim makesSize As Int
    makesSize = Round2((makeList.Size / 4),0)
    For i = 0 To (makeList.Size - 1) Step 4
        Dim id1 As ImageData = CreateItem2(makeList.GetKeyAt(i))
        Dim id2 As ImageData = CreateItem2(makeList.GetKeyAt(i+1))
        Dim id3 As ImageData = CreateItem2(makeList.GetKeyAt(i+2))
        Dim id4 As ImageData = CreateItem2(makeList.GetKeyAt(i+3))
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0, 0, zCLV_Vehicle.AsView.Width, 140dip)
        zCLV_Vehicle.Add(p, Array As ImageData(id1, id2, id3, id4))
    Next
 

mangojack

Expert
Licensed User
Longtime User
I could not test this completely .. but at the least it might offer an alternative way of thinking.

B4X:
    Dim ID(4) As ImageData
    Dim counter As Int = 0
    
    For i = 0 To makeList.Size - 1        
        ID(counter) = CreateItem2(makeList.GetKeyAt(i))

        If counter = 3 Or i = makeList.size -1 Then            
            Dim p As B4XView = xui.CreatePanel("")
            p.SetLayoutAnimated(0, 0, 0, zCLV_Vehicle.AsView.Width, 140dip)
            zCLV_Vehicle.Add(p, Array As ImageData(ID(0), ID(1), ID(2) ID(3)))
            Dim ID(4) As ImageData
            counter = 0
        Else
            counter = counter +1
        End If
    Next
 
Upvote 0
Top