Android Question Setting tag property dynamically

hakha4

Member
Licensed User
Longtime User
I have a need for setting tag property for buttons from a SQLlite table when program starts but can't figure out how to iterate trough control's and get a button's name property. Is this doable ? Tried to find info in beginners guide and forum without success

B4X:
Sub PopulateTagList()
    Dim ls As List
    ls.Initialize
    DBUtils.ExecuteList2(SQL1,"SELECT Button FROM IR_codes",Null,0,ls)' Button name property
    
    For i = 0 To ls.Size - 1
         Private tmptext As String
        tmptext = ls.Get(i)
        ' iterate trough buttons and put IR_code in button.tag when  NAME MATCH IS FOUND
        
        Log(tmptext)
                  
          
          
    Next

End Sub
Any help appreciated
Regards
 

Geezer

Active Member
Licensed User
Longtime User
Something like ...

B4X:
        For Each vw As View In Activity.GetAllViewsRecursive
            If vw Is Button Then
                Dim Bn As Button = vw
                ' If button is the right one, tag it
            End If
        Next
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
You can't refer to a view by name in code. If you only need to set the tag once (eg: when the app starts), you could initially set the tag with the button name, then use that as the reference.

Another option would be to create a class that subclasses the button & adds a name property to it. If you're going to do that you could also add an IR code property & use that instead of assigning it to the tag property of the button.

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
B4X:
'clsCustomButton
Sub Class_Globals
    Private mName As String
    Private mIRCode As String = ""
    Private mButton As Button
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(act As Activity, left As Int, top As Int, width As Int, height As Int, name As String, label As String)
    mName = name
    mButton.Initialize("button")
    mButton.Text = label
    act.AddView(mButton, left, top, width, height)
     
   
End Sub
Public Sub getIRCode() As String
    Return mIRCode
End Sub

Public Sub setIRCode(code As String)
    mIRCode = code
End Sub

Public Sub getNamne() As String
    Return mName
End Sub

Private Sub button_Click
    Log($"You clicked button ${mName}"$)
End Sub


'Main Activity
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private cButton(2) As clsCustomButton
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")
   
    cButton(0).Initialize(Activity, 10dip, 10dip, 120dip, 40dip, "Button 1", "Tap Me")
    cButton(1).Initialize(Activity, 10dip, 60dip, 120dip, 40dip, "Button 2", "Tap Me")
    cButton(0).IRCode = "ABCD1234"
    Log(cButton(0).IRCode)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub PopulateTagList()
    Dim ls As List
    ls.Initialize
    DBUtils.ExecuteList2(SQL1,"SELECT Button FROM IR_codes",Null,0,ls)' Button name property
    
    For i = 0 To ls.Size - 1
         Private tmptext As String
        tmptext = ls.Get(i)
        ' iterate trough buttons and put IR_code in button.tag when  NAME MATCH IS FOUND
        For j = 0 to cButton.Length - 1
            If cButton(j).Name = tmpText Then
                cButton(j).IRCode = 'whatever your IR code is
            End If
        Next
        Log(tmptext)
    Next

End Sub

[CODE]

You get the general idea...

- Colin.
 
Upvote 0
Top