Let's say I have 10 labels named "label0", "label1" and so on... and a list with 10 strings "listNames(10) As String" and I want programmatically send the 10 strings that are in the list at every label depending on their name...
is it possible to shorten the code doing something like this?:
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim Label() As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
Dim Label(10) As Label
For i = 0 To 9
Label(i).Initialize("")
Label(i).text = listNames(i)
Activity.AddView(Label(i),0dip,(i+1)*10dip,350dip,10dip)
Next
End Sub
if you give them a tag value like label0 label1 etc you could run through all the views and check if the tag equals the loop value and set the text if needed.
this works for both designer or scripted added views.
Thanks Sorex, I got your point of using tags... I only don't have clear how to use them with getallviewsrecursive... I tryed to search for an example but all I found made me more confused.... could you give me an example?
Thanks a lot
for x=0 to 9
setLabelText("label"&x,labeltexts(x))
next
sub setLabelText(lTag as string,lText as string)
For Each v As View In pnlLabels.GetAllViewsRecursive
If v.tag=lTag Then v.text=lText
Next
end sub
Better check it it is really a label...
And you need to bind the "v" to an objectinstance (dim)
B4X:
sub setLabelText(lTag as string,lText as string)
For Each v As View In pnlLabels.GetAllViewsRecursive
if v is Label then
dim lbl as Label = v
If v.tag=t Then v.text=lText
Next
end if
As the Labels already exist, I would do it that way:
B4X:
Dim Labels(10) As Label
Labels = Array As Label (label0, label1, label2, label3, label4, label5, label6, label7, label8, label9)
For i = 0 To 9
Labels(i).text = listNames(i)
Next
As the Labels already exist, I would do it that way:
B4X:
Dim Labels(10) As Label
Labels = Array As Label (label0, label1, label2, label3, label4, label5, label6, label7, label8, label9)
For i = 0 To 9
Labels(i).text = listNames(i)
Next