Android Question array of labels

sina39000

Member
My code:
B4X:
Sub Globals
    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Private Label4 As Label
    Private Label5 As Label

    Private LLL() As Label
    LLL = Array As Label (Label1,Label2, Label3, Label4, Label5)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    For i = 1 To 4
        LLL(i).Initialize("L"&i)
        LLL(i).Text = "Label No "&i
        LLL(i).Color = Colors.Blue
    Next
End Sub

Label1 to Label5 defined in designer
nothing on run and labels do not changes
what is wrong???
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

klaus

Expert
Licensed User
Longtime User
As Don Manfred allready said, you must NOT initialize the Labels again!
In your code, Label5 will not be changed.
Change For i = 1 To 4 to For i = 0 To 4 !
If you use any event with the Labels you should use the Tag property and set the Event Name the same for all Labels in the Designer.
And in the Event routine use the Sender keyword to get the calling Label.
 
Upvote 0

sina39000

Member
WHY are you Initialize them then? Views added through the designer (and loaded by LoadLayout) do not need to get initialized again...

Comment out the line

if i don`t initialize i get this Err

Error occurred on line: 35 (Main)
java.lang.RuntimeException: Object should first be initialized (Label).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.TextViewWrapper.setText(TextViewWrapper.java:39)
at b4a.example.main._activity_create(main.java:399)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at b4a.example.main.afterFirstLayout(main.java:104)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
 
Upvote 0

sina39000

Member
Scenario:
I Have many Labels on Designer
I want to change some properties from code by a loop and array of labels
please say to me what to do?
 
Upvote 0

mw71

Active Member
Licensed User
Longtime User
B4X:
......
    Dim lSpinner As List
    lSpinner.Initialize   
    lSpinner.AddAll(Array(Spinner1,Spinner2,Spinner1_E,Spinner2_E,spn_e_QSL_S, spn_QSL_S))
......

    For o=0 To lSpinner.Size-1   
        resetUserFontScale_Spinner(lSpinner.Get(o),varSpnSize)' varSpnSize is the new .TextSize
        'in this Sub i cach the Spinner Data, Clear, Resize and fill new the Spinner.....
    Next
......

'not testet, you must do that with your Labels
dim lbl as label
for o=0 to lSpinner.Size-1
     lbl=lSpinner.Get(o)
     lbl.TextSize=12
     .....
next

or use for each
 
Upvote 0
Top