I'm trying to update text of a label in a layout created with the Designer. I need to do this in code with an array. I can do this with a simple variable but not with an array. I tried the following without success. It doesn't generate any errors but the text never displays. I have done this before in a layout generated in code but not with a Designer layout.
B4X:
Sub Globals
Dim ThisField(10) as Label
...
' Sports is the name of the label in the layout
Activity.LoadLayout("MyLayout")
ThisField(1).Initialize("Sports")
ThisField(1).Text = "Baseball"
This didn't work. Although it compiled properly I got the runtime error "Object should first be initialized (Label)". I tried putting the array elements in quotes since they are strings but that caused a "Types do not match" error.
All this has been done. I have lots of experience with layouts through both the designer and in code but never with adding text from an ARRAY in code to a layout created in the Designer. Here are the pertinent lines from my program. Sports, Religion and Education are all the names of labels added in the designer layout.
B4X:
Sub Globals
Dim Sports As Label
Dim Religion As Label
Dim Education As Label
Dim ThisField(3) as Label
ThisField = Array as Label(Sports, Religion, Education)
...
Activity.LoadLayout("MyLayout")
ThisField(0).Text = "Baseball"
I still get the run time error "Object should first be initialized (Label)".
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Sports As Label
Private Religion As Label
Private Education As Label
Dim ThisField(3) As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("LayoutLabels")
ThisField = Array As Label(Sports,Religion,Education)
ThisField(0).Text = "Test"
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private Sports As Label
Private Religion As Label
Private Education As Label
Dim ThisField(3) As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("LayoutLabels")
ThisField = Array As Label(Sports,Religion,Education)
ThisField(0).Text = "Test"
Yes, that did it. Thanks. I tried every possible variation... except that one. Is this documented somewhere? I lost an 8 hour work day trying to figure this out.
Welcome to the world of developers. I promise you WILL "lose" much more time than this solving a problem The time isn't lost. It is a time to learn new things.