Android Question Can't seem to pass tag for Toggle Button

David Elkington

Active Member
Licensed User
Longtime User
Hope someone can help.

I am dynamically adding toggle buttons to a panel from a database, but I would like to pass the Tag to the click event as it basically decides whether it will be tri-state or Red on "No" for critical entries. I have the code below

<code>
Dim b AsToggleButton

Dim sf AsStringFunctions

sf.Initialize
b = Sender
b.Initialize("")

Select Case sf.Left(b.Tag,3)
Case"TG2" 'Two way, not critical

Case"TG3" 'Two way, critical

If b.Checked = False Then

b.Color=Colors.Red

b.Tag=sf.left(b.Tag,11) & "RD"

Else

b.Color = Colors.DarkGray

b.Tag=sf.left(b.Tag,11) & "DG"

EndIf

</code>

But when I interrogate b.tag, it says it is null? Am I doing something wrong.?
 

Cableguy

Expert
Licensed User
Longtime User
don't initialize the toggle button!

b.Initialize("")

that way tou're re-creating a blank togglebutton and deleting all the references from the sender.

[oh, and the code tags use straight edge brackets "[ ]" isntead of the javscript like ones you used "< >"]
 
Upvote 0

David Elkington

Active Member
Licensed User
Longtime User
When I take the initialise out, it says this,

java.lang.RuntimeException: Object should first be initialized (ToggleButton).
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
you could set it as a single line, like
B4X:
dim b as togglebutton = sender

also, if it still complains about not being initialized, try it like

B4X:
dim b as togglebutton
b.initialize("")
b=sender
note how we initialize the button BEFORE setting as a reference from the sender
 
Upvote 0

David Elkington

Active Member
Licensed User
Longtime User
Many thanks. your first suggestion worked, the second seemed to do the same as I was seeing, the one that worked was

dim b as togglebutton = sender
 
Upvote 0

David Elkington

Active Member
Licensed User
Longtime User
Saying that though, when I clicked the button for the second time, I got the Not Initialised Error again... something odd here.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
how and were are you creating your togglebuttons?
 
Upvote 0

David Elkington

Active Member
Licensed User
Longtime User
I was doing it like this,


<code>
Sub Globals

'These global variables will be redeclared each time the activity is created.

'These variables can only be accessed from this module.

Private pnl AsPanel

Private scrChecklist As ScrollView

Private lngYPos As Long

Private tglQuestion As ToggleButton

Private lbl AsLabel

Private txt AsEditText

EndSub
</code>



<code>
'Toggle Button

If cursor.Getint("chk_button_type_ID") = 0 Then

'No Button

Else

tglQuestion.Initialize("toggle")



If cursor.Getint("chk_button_type_ID") = 1 Then

tglQuestion.Tag = "tg2" & strID & "DG"

If cursor.Getint("chk_critical") = 0 Then

'Not critial

tglQuestion.Tag = "tg2" & strID & "DG"

tglQuestion.Color=Colors.DarkGray



Else

tglQuestion.Tag = "tg3" & strID & "RD"

tglQuestion.Color=Colors.Red

End If

Else

'Tri Stat

If cursor.Getint("chk_critical") = 0 Then

'Not critial

tglQuestion.Tag = "tg4" & strID & "DG"

tglQuestion.Color=Colors.DarkGray

Else

tglQuestion.Tag = "tg5" & strID & "RD"

tglQuestion.Color=Colors.Red

End If

End If



pnl.AddView(tglQuestion, lngToggleLeft * 1%x, lngYPos, 60dip, 30dip*Starter.dblButtonHeight)

tglQuestion.Checked=False

tglQuestion.TextOn="Yes"

tglQuestion.TextOff="No"

tglQuestion.TextSize=20*Starter.dblScaleFactor



End If

</code>
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
you keep using "< />" as tags for posting code, you should change them with "[ /]"

are your toggle buttons added in code or in the designer?
If they are added in the designer, YOU SHOULD NOT INTITIALIZE THEM!

after taking a better look at your code (it's hard to read the code without the correct tags) I see you are adding the togglebuttons by code, so I guess the error is somewere else in your code.
 
Upvote 0

David Elkington

Active Member
Licensed User
Longtime User
Sorry, thought it was html... My toggle buttons are not added in the designer as there could be 1 or there could be 1000 depending on how the questionnaire is formulated from the table.

Here is the complete code.

B4X:
#Region  Activity Attributes 

    #FullScreen: False

    #IncludeTitle: True

#End Region



Sub Process_Globals

    'These global variables will be declared once when the application starts.

    'These variables can be accessed from all modules.

    Dim reqManager AsDBRequestManager

   

EndSub



Sub Globals

    'These global variables will be redeclared each time the activity is created.

    'These variables can only be accessed from this module.

    Private pnl AsPanel

    Private scrChecklist As ScrollView

    Private lngYPos As Long

    Private tglQuestion As ToggleButton

    Private lbl AsLabel

    Private txt AsEditText

EndSub



Sub Activity_Create(FirstTime As Boolean)

   

    Activity.LoadLayout("Checklist")

    scrChecklist.Initialize(0)

    pnl = scrChecklist.Panel

    Activity.AddView(scrChecklist, 0, 0, 100%x, 100%y)

    scrChecklist.Color = Colors.white

   

    Activity.Title="Vehicle Checklist"

   

    'Read all the values from the table for this checklist, order by the ID as they will have all been set.

    'Key

    '

    ' 1= 

    ' 2= 

    ' 3= Comment

    ' 4= Toggle Button

    ' 5= Comment

    ' 6= Overall comment

   

   

    'Gather all the different values

    'Vehicle Type

    'Vehicle Trailer

    'Number of Sections

    'Number of questions in each section

   

    Dim WhereFields As Map

    Dim lngCountChecklist As Long

    Dim lngCheckListID As Long

    Dim lngType As Long

    Dim cursor As Cursor

   

    Dim lngSpace1 As Long

    Dim lngSpace2 As Long

    Dim lngSpace3 As Long

    Dim lngSpace4 As Long

    Dim lngSpaceBeforeSection As Long

    Dim lngSpaceBeforeVehicleHeader As Long

    Dim bolFirstSectionHeader As Boolean=True

    Dim bolFirstVehicleType As Boolean=True

    Dim lngToggleLeft As Long

    Dim lngToggleVertical As Long

    Dim sf AsStringFunctions

    Dim strID As String

   

    sf.Initialize

   

    lngCheckListID = Starter.SQL1.ExecQuerySingleResult("Select cur_Checklist from tbl_current_position where cur_ID=1")

    lngCountChecklist=Starter.SQL1.ExecQuerySingleResult("Select count(chk_id) from tbl_vcl_checklist where chk_checklist_ID=" & lngCheckListID)

    If lngCountChecklist>0 Then

        'reqManager.Initialize(Me, Starter.SQLStat.ExecQuerySingleResult("Select par_jServer_IP from Parameters where par_id=1"))

        cursor = Starter.SQL1.ExecQuery("Select * from tbl_vcl_checklist where chk_checklist_ID=" & lngCheckListID & " order by chk_ID")

   

        lngYPos = 20dip

        lngSpace1 = 30dip

        lngSpace2 = 35dip

        lngSpace3 = 35dip

        lngSpace4 = 35dip

        lngSpaceBeforeSection = 10dip

        lngSpaceBeforeVehicleHeader = 20dip

        lngToggleLeft = 50

       

       

        For i = 0 To cursor.RowCount-1

            cursor.Position=i

       

            lngType = cursor.Getint("chk_type_ID")

            strID = sf.pad(cursor.Getint("chk_ID"),"0",7,False)

           

            Select Case lngType

                Case  1        'Vehicle Type Header 

                    If bolFirstSectionHeader = True Then

                        bolFirstSectionHeader=False

                    Else

                        lngYPos=lngYPos+lngSpaceBeforeVehicleHeader

                    End If

                    lbl.Initialize("label")

                    lbl.Tag = "typ" & strID

                    pnl.AddView(lbl, 1%x, lngYPos, 98%x, 30dip)

                    lbl.Text="  " & cursor.Getstring("chk_text")

                    lbl.TextColor=Colors.White

                    lbl.color=Colors.ARGB(255, 0, 0, 128)

                    lbl.TextSize = 24 * Starter.dblScaleFactor

                    lbl.Gravity =Gravity.CENTER_VERTICAL

                    lngYPos = lngYPos + lngSpace1

                   

                Case  2        'Section Header

                    If bolFirstSectionHeader = True Then

                        bolFirstSectionHeader=False

                    Else

                        lngYPos=lngYPos+lngSpaceBeforeSection

                    End If

                   

                   

                    lbl.Initialize("label")

                    lbl.Tag = "sec" & strID

                    pnl.AddView(lbl, 1%x, lngYPos, 98%x, 30dip)

                    lbl.Text= " " & cursor.Getstring("chk_text")

                    lbl.TextColor=Colors.black

                    lbl.Typeface = Typeface.DEFAULT_BOLD

                   

                    lbl.TextSize = 22 * Starter.dblScaleFactor

                    lbl.Gravity =Gravity.CENTER_VERTICAL

                    lngYPos = lngYPos + lngSpace2

                   

                Case  3        ' Directive at Question Level

                    lbl.Initialize("label")

                    lbl.Tag = "dir" & strID

                    pnl.AddView(lbl, 1%x, lngYPos, 98%x, 30dip)

                    lbl.Text="    " & cursor.Getstring("chk_text")

                    lbl.TextColor=Colors.DarkGray

                    'lbl.color=Colors.ARGB(255, 0, 0, 128)

                    lbl.TextSize = 20 * Starter.dblScaleFactor

                    lbl.Gravity =Gravity.CENTER_VERTICAL

                    lbl.Typeface = Typeface.DEFAULT_BOLD

                    lngYPos = lngYPos + lngSpace3

               

                Case  4        'Question

                    'Question Text

                    lbl.Initialize("label")

                    lbl.Tag = "que" & strID

                    pnl.AddView(lbl, 1%x, lngYPos, 98%x, 30dip)

                    lbl.Text="    " & cursor.Getstring("chk_text")

                    lbl.TextColor=Colors.DarkGray

                    lbl.TextSize = 20 * Starter.dblScaleFactor

                    lbl.Gravity =Gravity.CENTER_VERTICAL

                   

                    'Toggle Button

                    If cursor.Getint("chk_button_type_ID") = 0 Then

                        'No Button

                    Else

                        tglQuestion.Initialize("toggle")

                       

                        If cursor.Getint("chk_button_type_ID") = 1 Then

                            tglQuestion.Tag = "tg2" & strID & "DG"

                            If cursor.Getint("chk_critical") = 0 Then

                                'Not critial

                                tglQuestion.Tag = "tg2" & strID & "DG"

                                tglQuestion.Color=Colors.DarkGray

                       

                            Else

                                tglQuestion.Tag = "tg3" & strID & "RD"

                                tglQuestion.Color=Colors.Red

                            End If

                        Else

                            'Tri Stat

                            If cursor.Getint("chk_critical") = 0 Then

                                'Not critial

                                tglQuestion.Tag = "tg4" & strID & "DG"

                                tglQuestion.Color=Colors.DarkGray

                            Else

                                tglQuestion.Tag = "tg5" & strID & "RD"

                                tglQuestion.Color=Colors.Red

                            End If

                        End If

                       

                        pnl.AddView(tglQuestion, lngToggleLeft * 1%x, lngYPos, 60dip, 30dip*Starter.dblButtonHeight)

                        tglQuestion.Checked=False

                        tglQuestion.TextOn="Yes"

                        tglQuestion.TextOff="No"

                        tglQuestion.TextSize=20*Starter.dblScaleFactor

                       

                    End If

                   

                    'Comment Box

                    If cursor.Getint("chk_comment_type_ID") = 0 Then

                        'No Comment Box

                    Else

                        txt.Initialize("comment")

                        txt.Tag = "qtx" & cursor.Getint("chk_ID")

                        pnl.AddView(txt, 75%x, lngYPos, 22%x, 40dip)

                        txt.TextColor=Colors.DarkGray

                        txt.TextSize = 20 * Starter.dblScaleFactor

                        txt.Gravity =Gravity.CENTER_VERTICAL

                    End If

                   

                    lngYPos = lngYPos + lngSpace4

               

            EndSelect

            Next

   

    EndIf

    For Each v As View In Activity.GetAllViewsRecursive

        If v Is Button Then

          Dim jo As JavaObject = v

       

          jo.RunMethod("setPadding", Array As Object(0, 0, 0dip, 3dip))

        EndIf

Next

    pnl.Height = 60dip +lngYPos

EndSub



Sub toggle_Click

   

    Dim b AsToggleButton = Sender

    Dim sf AsStringFunctions

   

    sf.Initialize

   



   

    Select Case sf.Left(b.Tag,3)

       

        Case"tg2"    'Two way, not critical

           

        Case"tg3"    'Two way,  critical

            If b.Checked = False Then

                b.Color=Colors.Red

                b.Tag=sf.left(b.Tag,11) & "RD"

            Else

                b.Color = Colors.DarkGray

                b.Tag=sf.left(b.Tag,11) & "DG"

            EndIf

           

        Case"tg4"    'Three way, not critical

            If b.TextOff = "N/A" Then

                b.TextOff="No"

            Else

                b.TextOff="N/A"

            EndIf

           

           

        Case"tg5"    'Three way,  critical

            If b.TextOff = "N/A" Or b.TextOn = "N/A" Then

                b.Color = Colors.DarkGray

                b.Tag=sf.left(b.Tag,11) & "DG"

            Else

       

            If b.Checked = False Then

                b.Color=Colors.Red

                b.Tag=sf.left(b.Tag,11) & "RD"

            Else

                b.Color = Colors.DarkGray

                b.Tag=sf.left(b.Tag,11) & "DG"

            EndIf

        End If       

       

       

    EndSelect

       

   

       

       

   

EndSub











Sub Activity_Resume



EndSub



Sub Activity_Pause (UserClosed As Boolean)



EndSub
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
B4X:
Sub toggle_Click

 
Dim b AsToggleButton = Sender
Dim sf AsStringFunctions

 

 sf.Initialize

Is this how you have in the real code?
Dim b AsToggleButton = Sender

there should be a space after the "as"
Dim b As ToggleButton = Sender
 
Upvote 0

David Elkington

Active Member
Licensed User
Longtime User
It does have a space in the code, for some reason it missed the space in the copy and paste. I am using a VM on a Mac so it could be a paste issue
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
honestly, I can't see the error... can you export the whole project? (file -> export as zip)
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
sometimes it does that, that's why a ctrl+P is so important at times... (Clean Project)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…