Android Question Using Loop Counter as part label name

Roger Daley

Well-Known Member
Licensed User
Hi All,

I have a number of labels [lblF0 - lblF15], the TextColor of these labels is changed programatically.
When I build a LIST of these labels I want the list Textcolor to match the laabel text color.
When I try to build the list using a loop I can't figure out how to make the Label name using the loop counter. [IE lblF&i doesn't work.] I suspect that it could be done using a java object haven' been able to find anything is searching.

Any help much appreciated.


B4X:
Sub FNumSelect
'Brings up ListView to allow selection of F0-F15 as assigned function.
'Called by Formula_click
    ListFlag = 1
    Dim FNumLabel As Label                            'Set the parameters for displaying the Items of the ListView
    FNumLabel = FNumView.SingleLineLayout.Label    '
    FNumLabel.TextSize = 18*FontScale                        '
    'FNumLabel.TextColor = Colors.White                '
    FNumLabel.Color = Colors.DarkGray                '
    FNumLabel.Height = 5.6%y                         'Label height and Item Height should match to avoid gsps & overlaps
       FNumView.SingleLineLayout.ItemHeight = FNumView.SingleLineLayout.Label.Height
    FNumView.Color = Colors.DarkGray                'ListView color matches Label color as labels don't fill ListView
    FNumView.Clear                                    'Clear Listview before populating or else you get duplicates
 
  
' This is the bit where I need help
    For i = 0 To 15                                    'Builds the viewable list RecallView from the List "storelist"
        FNumLabel.TextColor = lblF&i.TextColor        'Assign LIST Label textcolour from existing label textcolour???????????????
       FNumView.AddSingleLine("F" & i)             
      Next
 
  

    Activity.AddView(FNumView, 0, 10%y, 100%x, 90%y)   'Displays the list built by the previous loop.
    lblTitle.Text = "Select a Function Key to Program"
    lblTitle.TextSize = 14*FontScale
    lblTitle.BringToFront
    lblTitle.Height = 10%y
    ListBack.BringToFront
    ListBack.Height = 10%y
    FNumView.BringToFront
End Sub

Regards Roger
 
Last edited:

eurojam

Well-Known Member
Licensed User
Roger,
I think it better to work with an Array of labels to achieve what you want. something like
B4X:
    Dim lblF(16) As Label
    For i = 0 To 15                                   
       FNumLabel.TextColor = lblF(i).TextColor        'can you see the difference?
       FNumView.AddSingleLine("F" & i)            
    Next
 

Roger Daley

Well-Known Member
Licensed User
Thanks eurojam this looks good.
I guess I painted myself in to a corner not dimensioning as an array at the start. I will need to back track but well worth the effort and less complex than I was expecting.

Thanks for the enlightenment.

Regards Roger
 

Roger Daley

Well-Known Member
Licensed User
Well it looked good but I have fallen in to another pit.
Changed from DIMensioning individual labels to an array, modified code to suite and tried to compile with the attached result. The Save Settings in Activity Pause has the same green stripe.

I've tried researching and changing things, found nothing, achieved nothing. Any ideas?


Regards Roger


Error screen.jpg
 

JordiCP

Well-Known Member
Licensed User
Which error do you get?

The labels should be previously initialized if they have not been added in the designer.
 

MaFu

Well-Known Member
Licensed User
Another suggestion:
B4X:
For i = 0 To 10
    lblF(i).Initialize("")
    lblF(i).Text = StateManager.GetSetting2("lblF" & i & ".Text", "F" & i)
    lblF(i).TextColor = StateManager.GetSetting2("lblF" & i & ".TextColor", Colors.Gray)
Next
 

Roger Daley

Well-Known Member
Licensed User
JordiCP

Attached screenshot is best description.
The labels were created in the designer. As shown in the screenshot labels F0-F15 were originally DIMed individually but now as an array, attempting to solve problem above.
I have tried to initialize [lblF(15).Initialize] which gives me a "Missing Parameter" error. I have always created labels in the designer so missing something in initializing.

Regards Roger

Error screen2.jpg
 

MaFu

Well-Known Member
Licensed User
If you create the layout in designer the label variables must have the same name as the labels in designer.
Try this:
B4X:
Sub Globals
    ...
    Dim lblF0, lblF1, lblF2, lblF3, lblF4, lblF5, lblF6, lblF7, lblF8, lblF9 As Label
    Dim lblF10, lblF11, lblF12, lblF13, lblF14, lblF15 As Label
    Dim lblF(16) As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Portrait")
    lblF(0) = lblF0
    lblF(1) = lblF1
    lblF(2) = lblF2
    lblF(3) = lblF3
    lblF(4) = lblF4
    lblF(5) = lblF5
    lblF(6) = lblF6
    lblF(7) = lblF7
    lblF(8) = lblF8
    lblF(9) = lblF9
    lblF(10) = lblF10
    lblF(11) = lblF11
    lblF(12) = lblF12
    lblF(13) = lblF13
    lblF(14) = lblF14
    lblF(15) = lblF15
    For i = 0 To 15
        lblF(i).Text = StateManager.GetSetting2("lblF" & i & ".Text", "F" & i)
        lblF(i).TextColor = StateManager.GetSetting2("lblF" & i & ".TextColor", Colors.Gray)
    Next
    ...
End Sub
 

JordiCP

Well-Known Member
Licensed User
JordiCP

Attached screenshot is best description.
The labels were created in the designer. As shown in the screenshot labels F0-F15 were originally DIMed individually but now as an array, attempting to solve problem above.
I have tried to initialize [lblF(15).Initialize] which gives me a "Missing Parameter" error. I have always created labels in the designer so missing something in initializing.

Regards Roger

View attachment 35011
As MaFu's answer points, you don't need to initialize the array as new label objects if they are already created with the designer. All you need to do is reference its index elements to the ones created with the designer. By doing this you can keep working with the designer but also have a convenient way to handle them as arrays.
 

RandomCoder

Well-Known Member
Licensed User
If you create the layout in designer the label variables must have the same name as the labels in designer.
Try this:
B4X:
Sub Globals
    ...
    Dim lblF0, lblF1, lblF2, lblF3, lblF4, lblF5, lblF6, lblF7, lblF8, lblF9 As Label
    Dim lblF10, lblF11, lblF12, lblF13, lblF14, lblF15 As Label
    Dim lblF(16) As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Portrait")
    lblF(0) = lblF0
    lblF(1) = lblF1
    lblF(2) = lblF2
    lblF(3) = lblF3
    lblF(4) = lblF4
    lblF(5) = lblF5
    lblF(6) = lblF6
    lblF(7) = lblF7
    lblF(8) = lblF8
    lblF(9) = lblF9
    lblF(10) = lblF10
    lblF(11) = lblF11
    lblF(12) = lblF12
    lblF(13) = lblF13
    lblF(14) = lblF14
    lblF(15) = lblF15
    For i = 0 To 15
        lblF(i).Text = StateManager.GetSetting2("lblF" & i & ".Text", "F" & i)
        lblF(i).TextColor = StateManager.GetSetting2("lblF" & i & ".TextColor", Colors.Gray)
    Next
    ...
End Sub
But doing it this way it means that you are declaring 32 labels when in reality you only want 16. The way that I do this is use a map. The key of the map can be the label name but in my case I just use the labels tag and make sure my tags are unique and the value is the actual label reference.
 

RandomCoder

Well-Known Member
Licensed User
It's 32 variables but only 16 labels. If i use lblF(0) = lblF0 then both variables points to the same label object.
I always struggle to get my head around this! :oops:

B4X:
Sub Globals
    ...
    Dim lblF0, lblF1, lblF2, lblF3, lblF4, lblF5, lblF6, lblF7, lblF8, lblF9 As Label
    Dim lblF10, lblF11, lblF12, lblF13, lblF14, lblF15 As Label
    Dim lblF(16) As Label
End Sub
The declaration above would suggest declaring 32 Labels. I realise that in the code you later reference one Label to another but that is still 32 actual Labels is it not? Whereas using a map would be 16 Labels and one Map holding just the reference to those Labels.
But like I said, this is always a little confusing to me no matter how many times I read up on it. :(
 

MaFu

Well-Known Member
Licensed User
"Dim lblA, lblB As Label" declares two variables, not the objects.
"lblA.Initialize("")" creates the label object and the variable holds a reference to it.
"lblB = lblA" assigns the same object to the second variable. It's only one object and two references to it.
 

Roger Daley

Well-Known Member
Licensed User
Hi All,

Solution to my original problem is below. [Almost]. Using the Labels Tag instead of the Label name allows use of the loop counter.
Belatedly I realized that you can't change the text colour of individual items in a Listview [FNumView], I will need to re-do as a CustomListView however due other priorities I will be offline for a few days son that is next weeks job.
The code below shows how the loop can take the text from the label and use it in the items of the listview, something similar should work for textcolor when using a customlistview.


Thanks for the many sugestions

Regards Roger

B4X:
Sub FNumSelect
'Brings up ListView to allow selection of F0-F15 as assigned function.
'Called by Formula_click
    ListFlag = 1
    Dim FNumLabel As Label                            'Set the parameters for displaying the Items of the ListView
    Dim lblText As String
    FNumLabel = FNumView.SingleLineLayout.Label    '
    FNumLabel.TextSize = 18*FontScale                        '
    FNumLabel.TextColor = Colors.White                '
    FNumLabel.Color = Colors.DarkGray                '
    FNumLabel.Height = 5.6%y                         'Label height and Item Height should match to avoid gsps & overlaps
       FNumView.SingleLineLayout.ItemHeight = FNumView.SingleLineLayout.Label.Height
    'FNumView.Color = Colors.DarkGray                'ListView color matches Label color as labels don't fill ListView
    FNumView.Clear                                    'Clear Listview before populating or else you get duplicates
   
   
    For i = 0 To 15                                    'Builds the viewable list RecallView from the List "storelist"
        For Each v As View In Activity.GetAllViewsRecursive
           If v Is Label Then
                Dim lbl As Label = v
                 If v.Tag = "F"&i Then
                     lblText = lbl.Text               
                    'FNumView.TextColor = lbl.TextColor  ---  Does not work because FNumView is Listview, needs to be a CustomListView
                 End If
           End If
        Next

       FNumView.AddSingleLine(lblText)   
'      FNumView.AddSingleLine("F" & i)               
      Next
   
   
    Activity.AddView(FNumView, 0, 10%y, 100%x, 90%y)   'Displays the list built by the previous loop.
    lblTitle.Text = "Select a Function Key to Program"
    lblTitle.TextSize = 14*FontScale
    lblTitle.BringToFront
    lblTitle.Height = 10%y
    ListBack.BringToFront
    ListBack.Height = 10%y
    FNumView.BringToFront
End Sub
 
Top