B4J Question 2-dimensional label array?

techknight

Well-Known Member
Licensed User
Longtime User
I am attempting to do a 2 dimensional label array (think of a grid of labels). But it doesnt work.

Here is my feeble attempt:
B4X:
'2 Dimensional Array
Sub CollectNodes2D(Tags() As String) As Label()
    Dim TagsIndex As Int
    Dim lblsList(Tags.length) As List
    Dim MaxListSize As Int
    For TagsIndex = 0 To Tags.Length-1
        lblsList(TagsIndex).Initialize
        For I = 0 To OutputWindow.RootPane.NumberOfNodes - 1
            Dim n As Node = OutputWindow.RootPane.GetNode(I)
            If n.Tag <> Null And n.Tag = Tags(TagsIndex) Then lblsList(TagsIndex).Add(n)
        Next
        If MaxListSize < lblsList(TagsIndex).Size Then MaxListSize = lblsList(TagsIndex).Size 'Grab our largest list so the array variable can hold it. 
    Next
    Dim lbls(Tags.length, MaxListSize) As Label'Declare a large enough 2D array 
    For TagsIndex = 0 To Tags.Length - 1
        For i = 0 To lbls.Length - 1
            Dim lbl As Label = lblsList(TagsIndex).Get(i)
            lbls(TagsIndex, i) = lbl
        Next
    Next
    Return lbls
End Sub

But it gets an error:
main.java:402: error: incompatible types: LabelWrapper[][] cannot be converted to LabelWrapper[]
if (true) return (anywheresoftware.b4j.objects.LabelWrapper[])(_lbls);

Thoughts?
 

stevel05

Expert
Licensed User
Longtime User
Can't test it but I would think that the sub signature should be:

B4X:
Sub CollectNodes2D(Tags() As String) As Label(,)
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
That did work, but a new problem surfaced.

the .Length function. the I = 0 to lbls.Length now is broken because its a 2D array. Problem is, I am unsure how to fix that.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Not sure how to do that. But after a little bit of thinking, I came to this and it worked finally:

B4X:
Sub CollectNodes2D(Tags() As String) As Label(,)
    Dim TagsIndex As Int
    Dim lblsList(Tags.length) As List
    Dim MaxListSize As Int
    Dim ListSize(Tags.Length) As Int
    For TagsIndex = 0 To Tags.Length-1
        lblsList(TagsIndex).Initialize
        For I = 0 To OutputWindow.RootPane.NumberOfNodes - 1
            Dim n As Node = OutputWindow.RootPane.GetNode(I)
            If n.Tag <> Null And n.Tag = Tags(TagsIndex) Then lblsList(TagsIndex).Add(n)
        Next
        ListSize(TagsIndex) = lblsList(TagsIndex).Size 'Grab our list size for this particular Tag.
        If MaxListSize < lblsList(TagsIndex).Size Then MaxListSize = lblsList(TagsIndex).Size 'Grab our largest list so the array variable can hold it.
    Next
    Dim lbls(Tags.length, MaxListSize) As Label'Declare a large enough 2D array
    For TagsIndex = 0 To Tags.Length-1
        For i = 0 To ListSize(TagsIndex) - 1
            Dim lbl As Label = lblsList(TagsIndex).Get(i)
            lbls(TagsIndex, i) = lbl
        Next
    Next
    Return lbls
End Sub

It may not be the best or most efficient piece of code out there, many ways to skin the cat, but it works for what I needed it for. I figured I would post it in case someone else wants to use it.

The reason I needed to do that, is so I could do this:
B4X:
    lblMats = CollectNodes2D(Array As String("1", "2", "3", "4", "5", "6", "7", "8"))
    lblMats(7,4).Text = "Hello"
 
Upvote 0
Top