Android Question Indirectly reference a view

Paul Boyle

Member
Licensed User
Longtime User
Hi

I have 20 labels in a panel - lbl_1Score, lbl_2Score ....... lbl_20Score.

There is a variable lng_Target which will iterate from 1 to 20 each time a button is clicked.

I would like to reference each of labels, dependent upon the lng_Target value and update the text of that specific label.

Example: When lng_Target = 1 I need to reference lbl_1Score, when lng_Target =2 I need to reference lbl_2Score, etc, updating the text of the label.

Is there code similar to :

With Views("lbl_" & lng_Target & "Score")
.Text = lng_Target
End With


Other wise I would have to write 20 Select Case lines.

Thanks for any replies.
Mosiki.
 

stevel05

Expert
Licensed User
Longtime User
A bit of manual work, but you could assign your labels to an array:

B4X:
Dim Labels() As Label  = Array As Labels(lbl1_Score,lbl2_Score,lbl_3Score) 'etc

Then Access them as:

B4X:
Labels(Ing_Target).Text = Ing_Target
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi all, out of curiosity.

Why not a simpler:
B4X:
Dim lblScores(20) as Label
....
lblScores(Ing_Target-1).Text = "whatever goes here"

udg
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
How would I go about that? Never used a map before.
Assuming you have a Button and a Label designed in your designer-layout

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 lv As ListView
    Private lbl_1Score As Label
    Private lbl_2Score As Label
    Private lbl_3Score As Label
    Private lbl_4Score As Label
    Private lbl_5Score As Label
    Private Button1 As Button
    Private EditText1 As EditText
    Dim objMap As Map
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("main")
    objMap.Initialize
    ' PLEASE NOTE that the Layout must be loaded FIRST before you put the objects into the map!
    ' We now add all Objects to the map giving them unique names
    ' We want to use them later in the button-click event
    objMap.Put("button1",Button1)
    objMap.Put("listview",lv)
    objMap.Put("score1",lbl_1Score)
    objMap.Put("score2",lbl_2Score)
    objMap.Put("score3",lbl_3Score)
    objMap.Put("score4",lbl_4Score)
    objMap.Put("score5",lbl_5Score)
    objMap.Put("Edit1",EditText1)

    For i = 0 To 49
        lv.AddSingleLine(i)
    Next
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub lv_ItemClick (Position As Int, Value As Object)
End Sub

and you want access them later in your code with a "string"

B4X:
Sub Button1_Click
    ' The button is clicked and we want to access our objects we stored in the map
    Dim et As EditText = objmap.Get("Edit1")
    et.Text = "New text ;-)"

    For i = 1 To 5
      Dim lbl As Label = objmap.Get("score"&i)
      lbl.Text = "New Labeltext ;-)"
    Next

End Sub

20 I will be updating
You need to put all 20 into the map for sure giving each one a unique name (or even a name you can "work with")
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
Yet another way to solve Paul's problem (absolutely not optimized but with the least impact on his actual code, I think).

Assuming each label in the panel has its Tag property set according to that label's name (e.g. Tag = 1Score for label lbl_1Score), he can simply cycle on each label in the panel with panel's property GetAllViewsRecursive.

B4X:
For Each lx As Label In YourPanelName.GetAllViewsRecursive
  if lx.Tag = Ing_Target &"Score" then lx.Text="any new text"
Next

udg
 
Upvote 0
Top