Android Question How to update label.Text from an array

DickD

Active Member
Licensed User
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"
 

DickD

Active Member
Licensed User
You should never initialize views added with the designer.

B4X:
Dim ThisField() as Label
ThisField = Array As Label(Sports, Politics, ...)
...
Line = "abc"
ThisField(0).Text = Line
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.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. Add the labels with the designer.
2. ADD REFERNCES to ALL OBJECTS IN THE LAYOUT.
editor0154.png

do this for all labels...
2. In your code you now add all the items to the array. See post #2
I tried putting the array elements in quotes since they are strings
NO! The problem is here.
B4X:
    Private Label1 As Label
Label1 is NOT A STRING.
Add Label1 to the array together with the other 9 Labelnames.
 
Upvote 0

DickD

Active Member
Licensed User
1. Add the labels with the designer.
2. ADD REFERNCES to ALL OBJECTS IN THE LAYOUT.

do this for all labels...
2. In your code you now add all the items to the array. See post #2

NO! The problem is here.
B4X:
    Private Label1 As Label
Label1 is NOT A STRING.
Add Label1 to the array together with the other 9 Labelnames.
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)".
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You need to load the layout FIRST

B4X:
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"
 
Upvote 0

DickD

Active Member
Licensed User
You need to load the layout FIRST

B4X:
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.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I tried every possible variation

No. For sure NOT

Is this documented somewhere?

See the beginners guide and how to work with views and of course to work with arrays. Of course you will not find your exact issue.

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.
 
Upvote 0
Top