B4J Question [ABMaterial] getActive not changing with ABMRadioGroup

Rob White

Member
Licensed User
Hi All,

I have the following page which has two groups of radio buttons neither returns correct or changing values from the click event for rbg.GetActive (always 0).

How can I get which button is the active button?

B4X:
'Class module
Sub Class_Globals
    Private ws As WebSocket 'ignore
    ' will hold our page information
    Public page As ABMPage
    ' page theme
    Private theme As ABMTheme
    ' to access the constants
    Private ABM As ABMaterial 'ignore  
    ' name of the page, must be the same as the class name (case sensitive!)
    Public Name As String = "Observ"  '<-------------------------------------------------------- IMPORTANT
    ' will hold the unique browsers window id
    Private ABMPageId As String = ""
    ' your own variables
    ' Any controls we need to pickup events from must be declared here.
    Dim cmbMobs As ABMCombo
    Dim cmbBeasts As ABMCombo
    Dim DP As ABMDateTimePicker
    Dim rbgroupB As ABMRadioGroup        ' Beast grouping
    Dim rbgroupO As ABMRadioGroup        ' observation type grouping
    Dim obsNotes As ABMInput
    Dim btnSave As ABMButton
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    ' build the local structure IMPORTANT!
    BuildPage      
End Sub

#Region ABM
Private Sub WebSocket_Connected (WebSocket1 As WebSocket)  
    Log("Connected")
       
    ws = WebSocket1      
   
    ABMPageId = ABM.GetPageID(page, Name,ws)
   
    Dim session As HttpSession = ABM.GetSession(ws, ABMShared.SessionMaxInactiveIntervalSeconds)
    If session.IsNew Then
        session.Invalidate
        ABMShared.NavigateToPage(ws, "", "./")
        Return
    End If
       
    If ABMShared.NeedsAuthorization Then
        If session.GetAttribute2("IsAuthorized", "") = "" Then
            ABMShared.NavigateToPage(ws, ABMPageId, "../")
            Return
        End If
    End If      
    ABM.UpdateFromCache(Me, ABMShared.CachedPages, ABMPageId, ws)      
    If page.ComesFromPageCache Then
        ' when we have a page that is cached it doesn't matter if it comes or not from a new connection we serve the cached version.
        Log("Comes from cache")      
        page.Refresh      
        page.FinishedLoading      
    Else
        If page.WebsocketReconnected Then
            Log("Websocket reconnected")
            ' when we have a client that doesn't have the page in cache and it's websocket reconnected and also it's session is new - basically when the client had internet problems and it's session (and also cache) expired before he reconnected so the user has content in the browser but we don't have any on the server. So we need to reload the page.
            ' when a client that doesn't have the page in cache and it's websocket reconnected but it's session is not new - when the client had internet problems and when he reconnected it's session was valid but he had no cache for this page we need to reload the page as the user browser has content, reconnected but we have no content in cache
            ABMShared.NavigateToPage (ws, ABMPageId, "./" & page.PageHTMLName)
        Else
            ' when the client did not reconnected it doesn't matter if the session was new or not because this is the websockets first connection so no dynamic content in the browser ... we are going to serve the dynamic content...
            Log("Websocket first connection")
            page.Prepare
            ConnectPage          
        End If
    End If
    Log(ABMPageId)      
End Sub

Private Sub WebSocket_Disconnected
    Log("Disconnected")
End Sub

Sub Page_ParseEvent(Params As Map)
    Dim eventName As String = Params.Get("eventname")
    Dim eventParams() As String = Regex.Split(",",Params.Get("eventparams"))
   
    Log($">>>>>>>>>>>>>>  ${eventName} - Fired"$)
   
    If eventName = "beforeunload" Then
        Log("preparing for url refresh")
        ABM.RemoveMeFromCache(ABMShared.CachedPages, ABMPageId)
        Return
    End If
    Dim caller As Object = page.GetEventHandler(Me, eventName)
    If caller = Me Then
        If SubExists(Me, eventName) Then
            Params.Remove("eventname")
            Params.Remove("eventparams")
            If eventName = "page_dropped" Then
                page.ProcessDroppedEvent(Params)
            End If
            Select Case Params.Size
                Case 0
                    CallSub(Me, eventName)
                Case 1
                    CallSub2(Me, eventName, Params.Get(eventParams(0)))
                Case 2
                    If Params.get(eventParams(0)) = "abmistable" Then
                        Dim PassedTables As List = ABM.ProcessTablesFromTargetName(Params.get(eventParams(1)))
                        CallSub2(Me, eventName, PassedTables)
                    Else
                        CallSub3(Me, eventName, Params.Get(eventParams(0)), Params.Get(eventParams(1)))
                    End If
                Case Else
                    ' cannot be called directly, to many param
                    CallSub2(Me, eventName, Params)
            End Select
        End If
    Else
        CallSubDelayed2(caller, "ParseEvent", Params) 'ignore
    End If
End Sub

public Sub BuildTheme()
    ' start with the base theme defined in ABMShared
    theme.Initialize("pagetheme")
    theme.AddABMTheme(ABMShared.MyTheme)

    ' add your specific page themes
   
End Sub

public Sub BuildPage()
    ' initialize the theme
    Log("At Observ.BuildPage -------------------------------------------------------------------------")
    BuildTheme
   
    ' initialize this page using our theme
    page.InitializeWithTheme(Name, "/ws/" & ABMShared.AppName & "/" & Name, False, ABMShared.SessionMaxInactiveIntervalSeconds, theme)
    page.ShowLoader=True
    page.PageHTMLName = "index.html"
    page.PageTitle = "Observation"
    page.PageDescription = "Record observation on specific beast"
    page.PageKeywords = ""
    page.PageSiteMapPriority = ""
    page.PageSiteMapFrequency = ABM.SITEMAP_FREQ_YEARLY
       
    page.ShowConnectedIndicator = True
               
    ' adding a navigation bar
    'ABMShared.BuildNavigationBar(page, "My Page","../images/logo.png", "", "", "")
           
    ' create the page grid
#Region C:\CattleMan\V1\Cman\Grids\observ.abmgrid
    'PHONE
    '╔═══════════════════════════════════════════════════════════════════════════════════╗
    '║ 1,1                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 2,1                                     | 2,2                                     ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 3,1                              | 3,2                                     |      ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 4,1                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 5,1                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════╣
    '║ 6,1                                                          | 6,2                ║
    '╚═══════════════════════════════════════════════════════════════════════════════════╝

    'TABLET
    '╔═══════════════════════════════════════════════════════════════════════════════════════════════════════════╗
    '║ 1,1                                                                                                       ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 2,1                                                 | 2,2                                                 ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 3,1                                        | 3,2                                                 |        ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 4,1                                                                                                       ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 5,1                                                                                                       ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 6,1                                                                            | 6,2                      ║
    '╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════╝

    'DESKTOP
    '╔═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
    '║ 1,1                                                                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 2,1                                                             | 2,2                                                             ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 3,1                                                  | 3,2                                                             |          ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 4,1                                                                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 5,1                                                                                                                               ║
    '╠═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
    '║ 6,1                                                                                              | 6,2                            ║
    '╚═══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

    page.AddRows(1,True,"").AddCells12(1,"")
    page.AddRows(1,True,"").AddCellsOS(2,0,0,0,6,6,6,"")
    page.AddRows(1,True,"").AddCellsOS(1,0,0,0,5,5,5,"").AddCellsOS(1,0,0,0,6,6,6,"")
    page.AddRows(2,True,"").AddCells12(1,"")
    page.AddRows(1,True,"").AddCellsOS(1,0,0,0,9,9,9,"").AddCellsOS(1,0,0,0,3,3,3,"")
    page.BuildGrid ' IMPORTANT!
#End Region
    Log("Grid built .......................")
   
End Sub

public Sub ConnectPage()          
    '    connecting the navigation bar
    'ABMShared.ConnectNavigationBar(page)
   
    Dim lblTitle As ABMLabel
    lblTitle.Initialize(page, "lblobservtitle", ABMShared.TitleStr("Observation"), ABM.SIZE_H3, False, "lbltheme1")
   
    cmbMobs.Initialize(page,"cmbmobs","Select mob",500,"")
       
    If Main.allMobs.Size <= 0 Then
        DBUtils.loadMobs(DBUtils.sql1,Main.allMobs)
    End If
    uiUtils.fillMobsCombo(page,cmbMobs,Main.allmobs)
    cmbBeasts.Initialize(page,"cmbbeasts","Select beast",500,"")
    cmbBeasts.Enabled=False
   
    DP.Initialize(page,"dpdate",ABM.DATETIMEPICKER_TYPE_DATE,DateTime.Now,"Observation on","")
    DP.CancelText = "Cancel"
    DP.PickText = "Ok"
    DP.TodayText = "Today"
    DP.ClearText = ""
    DP.FirstDayOfWeek = ABM.FIRSTDAYOFWEEK_MONDAY
    DP.ReturnDateFormat = "DD/MMM/YYYY"
    DP.ReturnTimeFormat=""
   
    Dim lblTitle1 As ABMLabel
    lblTitle1.Initialize(page, "lblobservtitle", "One", ABM.SIZE_H3, False, "lbltheme1")
    Dim lblTitle2 As ABMLabel
    lblTitle2.Initialize(page, "lblobservtitle", "Two", ABM.SIZE_H3, False, "lbltheme1")
    Dim lblTitle3 As ABMLabel
    lblTitle3.Initialize(page, "lblobservtitle", "Three", ABM.SIZE_H3, False, "lbltheme1")

    ' Beast groupings
    rbgroupB.Initialize(page, "rbgroupb", "")
    rbgroupB.Title = "Beast group (NOT implemented yet)"
    rbgroupB.AddRadioButtonNoLineBreak("All   ", True)
    rbgroupB.AddRadioButton("Cows   ", True)
    rbgroupB.AddRadioButtonNoLineBreak("Weaners   ", True)
    rbgroupB.AddRadioButton("Bulls", True)
    rbgroupB.SetActive(0)

    ' Observation type groupings
    rbgroupO.Initialize(page, "rbgroupo", "")
    rbgroupO.Title = "Observation type"
    rbgroupO.AddRadioButtonNoLineBreak("Birth", True)
    rbgroupO.AddRadioButton("General   ", True)
    rbgroupO.SetActive(0)

    ' Notes
    obsNotes.Initialize(page,"obsNotes",ABM.INPUT_TEXT,"Notes",True,"")
    obsNotes.Enabled=False
   
    btnSave.InitializeRaised(page, "btnsave", "", "", "Save", "bluegrey")
       
    page.Cell(1,1).AddComponent(lblTitle)
    page.Cell(2,1).AddComponent(cmbMobs)
    page.Cell(2,2).AddComponent(DP)
    page.Cell(3,1).AddComponent(cmbBeasts)
    page.Cell(3,2).AddComponent(rbgroupB)
    page.Cell(4,1).AddComponent(rbgroupO)
    page.Cell(6,1).AddComponent(obsNotes)
    page.Cell(6,2).AddComponent(btnSave)
    Log("End add components")
    ' Tell the browser we finished loading
    ' refresh the page
    page.Refresh
    page.FinishedLoading
    ' restoring the navigation bar position
    'page.RestoreNavigationBarPosition  
End Sub
#end region

Sub cmbMobs_clicked(itemId As String)
    Log($"cmbMobs click event itemID = ${itemId}"$)
    cmbBeasts.Enabled=True
    DBUtils.getBeastGrp(DBUtils.sql1,"MobID = " & itemId,Main.beastGroup)
    uiUtils.fillBeastsCombo(page,cmbBeasts,Main.beastGroup)

End Sub

Sub rbgroupo_Clicked(target As String)
    Log($"++++++++++ Inside rbgroupo_Clicked itemID = ${target}"$)
    setDefNote(rbgroupO.GetActive)
End Sub

Sub rbgroupb_Clicked(target As String)
    Log($"---------- Inside rbgroupb_Clicked itemID = ${target}"$)
    Log($"Active ID = ${rbgroupB.GetActive}"$)
End Sub

Sub setDefNote(ObsType As Int )
    Log($".......... Inside setDefNote(${ObsType})"$)
    Dim dateStr As String = DateTime.Date(DP.GetDate)
    Select Case ObsType
        Case 0
            obsNotes.Text = $"Birth of calf for ${Main.currBeast.EarTag} on ${dateStr} :- "$
        Case 1
            obsNotes.Text = $"General note for ${Main.currBeast.EarTag} made on ${dateStr} :- "$
    End Select
    obsNotes.Refresh
   
End Sub

Sub cmbBeasts_clicked(itemID As String)
    Log($"cmbBeasts click event itemID = ${itemID}"$)
    Main.currBeast = uiUtils.getBeastFromNLIS(itemID,Main.beastGroup)
    'Log($"cmbBeasts click event fired for ${uiUtils.beastIDstr(Main.currBeast)}"$)
    obsNotes.Enabled = True
    setDefNote(rbgroupO.GetActive)
End Sub

#Region ABMPage
' clicked on the navigation bar
Sub Page_NavigationbarClicked(Action As String, Value As String)
    ' saving the navigation bar position
    page.SaveNavigationBarPosition
    If Action = "LogOff" Then
        ABMShared.LogOff(page)
        Return
    End If

    ABMShared.NavigateToPage(ws, ABMPageId, Value)
End Sub

Sub Page_DebugConsole(message As String)
    Log("---> " & message)
End Sub
#end region
Here is a part log file where I have clicked alternate buttons:-

B4X:
>>>>>>>>>>>>>>  page_visibilitystate - Fired
>>>>>>>>>>>>>>  rbgroupo_clicked - Fired
++++++++++ Inside rbgroupo_Clicked target = rbgroupo
.......... Inside setDefNote(0)
>>>>>>>>>>>>>>  rbgroupo_clicked - Fired
++++++++++ Inside rbgroupo_Clicked target = rbgroupo
.......... Inside setDefNote(0)
>>>>>>>>>>>>>>  rbgroupo_clicked - Fired
++++++++++ Inside rbgroupo_Clicked target = rbgroupo
.......... Inside setDefNote(0)
>>>>>>>>>>>>>>  rbgroupo_clicked - Fired
++++++++++ Inside rbgroupo_Clicked target = rbgroupo
.......... Inside setDefNote(0)
Any thoughts anyone?
 

Harris

Expert
Licensed User
Sub rbgroupo_Clicked(target As String)
Log($"++++++++++ Inside rbgroupo_Clicked itemID = ${target}"$)
setDefNote(rbgroupO.GetActive)

End Sub

You did not define the object you want to set in the click event...
It requires a bit more code...

B4X:
Sub rbgroupo_Clicked(target As String)
    ' get the page object...
    Dim rbo As ABMRadioGroup = page.Component("rbgroupo")  'get the object we want to set...

    Log($"++++++++++ Inside rbgroupo_Clicked itemID = ${target}"$)

    ' may need to really set it......
     rbo.refresh

    setDefNote(rbo.GetActive)



End Sub
 

Rob White

Member
Licensed User
Thank you worked like a dream.

I believe all that is required is the refresh, rbgroupO.refresh works!
 
Last edited:
Top