Android Question Linking to firebase database

Rajesh Krishnan

Member
Licensed User
Hello,
Trying out on a feature where, while loading of an activity would want to return a set of labels from the firebase backend these are the 2 places i require help on -:
{
"Parents": {
"-KqRTcdg-F8rsl-_Z4kI": {
"Company": "Dell",
"Name": "Rajesh",
"Email": "[email protected]"
},
"HpW6r4ueEGR15oBLfVP04r5rVTB3": {
"Company": "Rapidd",
"Name": "Snehitha",
"Email": "[email protected]"
},
"-KqRTw6LBFk2jxpb-pKn": {
"Company": "Rapidd",
"Name": "Reshma",
"Email": "[email protected]"
},
"-KqRWTIzhh1Eb_atpkQN": {
"Company": "Rapidd",
"Name": "Anup",
"Email": "[email protected]"
},
"-KqRWjRNYlBTxZ75fCro": {
"Company": "Rapidd",
"Name": "Karthik",
"Email": "[email protected]"
}
},
"Students": 0
}
1) The above is my table structure so if i have to call to one user i would use the following url :
https://kpstudentapp.firebaseio.com/Parents/-KqRTcdg-F8rsl-_Z4kI/Company
now how do i make this a dynamic url ? for the purpose of managing many users and not entering their respective url everytime they login
2) I would want to call a few labels of the backend on load of an activity, how can i do that, right now i am able to do it via click of a button, but i want to improve this to loading as soon the page is loaded
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim strTarget As String = "Parents/HpW6r4ueEGR15oBLfVP04r5rVTB3/Company"
    Dim strText1 As String = ""
   
    Private auth As FirebaseAuth
End Sub


Sub Globals
    Private Button2 As Button
    Private EditText3 As EditText
    Private EditText2 As EditText
    Private Label1 As Label
    Private Label3 As Label
'    Private Label2 As Label
    Private Label4 As Label
    Private Label5 As Label
    Private Label5 As Label
    Private Label6 As Label
    Private Label7 As Label
    Private Label8 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("testfiba")
    If FirstTime Then
        auth.Initialize("auth")
    End If
    If auth.CurrentUser.IsInitialized Then Auth_SignedIn(auth.CurrentUser)
   
   
End Sub

Sub Activity_Resume
    Label7.Text = Starter.strFirebaseSignedInUser
   
'    EditText2.Text = strTarget 'Starter.kvs.GetDefault("fbdbPath", strTarget)
'    EditText3.Text = strText1  'Starter.kvs.GetDefault("fbdbValue", strText1)
    '
'    Label2.text = "(ready to send)"
'    Label8.text = "(ready to send)"
'    Label1.text = strTarget 'Starter.kvs.GetDefault("fbdbPath", strTarget)
'    Label6.text = "(ready to delete)"

    If Starter.strFirebaseProjectId.Contains(" ") Or Starter.strFirebaseProjectId.Contains(" ") Then
        Msgbox("*** You have to set up your Firebase parameters in the Starter-module", "Not ready for testing!")
        Return
    End If

       Starter.fbdb.Initialize(Me, $"https://${Starter.strFireBaseProjectId}.firebaseio.com/"$, "FiBaTest_Return")

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

'    ' fixed according to https://www.b4x.com/android/forum/threads/b4a-and-firebase-database.68118/page-3#post-443619
'    If Starter.auth.CurrentUser.IsInitialized Then
'        Auth_SignedIn(Starter.auth.CurrentUser )
'    Else
'        Starter.auth.SignInWithGoogle
'    End If
'    '   
'End Sub

'Sub Button2_Click  ' POST
'Log("#-Button2_Click")   
'     'Starter.kvs.put("fbdbPath", EditText2.Text)
'     'Starter.kvs.put("fbdbValue", EditText3.Text)
'
'    ' --- --- --- --- ---
'    ' POST to FiBaDb
'    ' --- --- --- --- ---
'    Label2.text = "(running...)"
'
'    Dim mapToSend As Map : mapToSend.Initialize
'    mapToSend.Put("myvalue1", EditText3.Text)
'   
'    Starter.fbdb.POST(EditText2.Text, mapToSend, "xyz POST-Test")
'   
'End Sub

'Sub Button5_Click ' PUT
'Log("#-Button5_Click")   
'     'Starter.kvs.put("fbdbPath", EditText2.Text)
'     'Starter.kvs.put("fbdbValue", EditText3.Text)
'
'    ' --- --- --- --- ---
'    ' PUT to FiBaDb
'    ' --- --- --- --- ---
'    Label8.text = "(running...)"
'
'    Dim mapToSend As Map : mapToSend.Initialize
'    mapToSend.Put("myvalue1", EditText3.Text)
'   
'    Starter.fbdb.PUT(EditText2.Text, mapToSend, "vvv PUT-Test")
'   
'End Sub



Sub Button1_Click ' GET
Log("#-Button1_Click")       
     'Starter.kvs.put("fbdbPath", EditText2.Text)
     'Starter.kvs.put("fbdbValue", EditText3.Text)

    ' --- --- --- --- ---
    ' Read from FiBaDb
    ' --- --- --- --- ---
'    Label1.text = "(running...)"


' -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
' uncomment ONE of the lines starting with "Starter.fbdb.GET..."
   
    ' GET standard
'Starter.fbdb.GET(EditText2.Text, "zzz GET-Test")
   
    ' GET with callback
'Starter.fbdb.GET_cb(EditText2.Text, "zzz GET-Test", "subtotest")
   
    ' GET with download
    Dim strFileDest As String = File.Combine(File.DirRootExternal, "aaa_fibadbtest/" & strTarget & ".json" )
    Starter.fbdb.GET_dl(strTarget, "zzz GET-Test", strFileDest )
' -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
End Sub   



Sub Label1_Click
    Dim strFileDest As String = File.Combine(File.DirRootExternal, "aaa_fibadbtest/" & strTarget & ".json" )
    Starter.fbdb.GET_dl(strTarget, "zzz GET-Test", strFileDest )
'    Msgbox(Label1.Text, "Content of Label1")
End Sub

Sub subtotest(mapRet As Map)
Log("#-subtotest, mapRet=" & mapRet)
    For Each n As String In mapRet.Keys
Log("#-  n=" & n & " --> " & mapRet.GetDefault(n, ""))           
    Next
   
End Sub



'Sub Button3_Click ' DELETE
'Log("#-Button3_Click")       
'     'Starter.kvs.put("fbdbPath", EditText2.Text)
'     'Starter.kvs.put("fbdbValue", EditText3.Text)
'
'    ' --- --- --- --- ---
'    ' DELETE in FiBaDb
'    ' --- --- --- --- ---
'    Label6.text = "(running...)"
'
'    If Msgbox2("Be careful! You may loose all your data if the path is not correct" & CRLF & CRLF & "DELETE '" & EditText2.Text & "' and all data below?", "Read me, ... no really, READ THE TEXT", "DELETE", "cancel","", Null) = DialogResponse.POSITIVE Then
'        Starter.fbdb.DELETE(EditText2.Text, "ppp DELETE-Test")
'    End If
'
'End Sub   

Sub FiBaTest_Return(mapRet As Map)
Log("#-FiBaTest_Return, mapRet=" & mapRet)
   
    Select Case True
'        Case mapRet.Get("Job.Tag") = "xyz POST-Test"
''            Label2.Text = mapRet.Get("Job.info")
''           
'        Case mapRet.Get("Job.Tag") = "vvv PUT-Test"
'            Label8.Text = mapRet.Get("Job.info")
           
        Case mapRet.Get("Job.Tag") ="zzz GET-Test"
            Label1.text = "(finished)"
            Label1.Text = mapRet.Get("Job.info")
           
            ' If a callback was defined for GET
            ' --> https://firebase.google.com/docs/reference/rest/database/#section-param-callback
            Dim strGS As String = mapRet.GetDefault("Job.Getstring", "")
            If strGS.StartsWith("/**/") Then
                strGS = strGS.Replace("/**/", "").Trim
                Dim intP As Int = strGS.IndexOf("(")
                If intP > -1 Then    
                    Dim strSub As String = strGS.SubString2(0, intP)
                    Dim strM As String   = strGS.SubString2(intP +1, strGS.Length -2)
                    Dim mapDat As Map : mapDat.Initialize
                    If strM <> "null" Then
                        Dim JSON As JSONParser : JSON.Initialize(strM)
                        mapDat = JSON.NextObject
                    End If
                    CallSubDelayed2(Me, strSub, mapDat)
                End If
            End If
           
'        Case mapRet.Get("Job.Tag") = "ppp DELETE-Test"
'            Label6.Text = "DELETE success=" & mapRet.Get("Job.Success")
           
    End Select
End Sub



Sub Auth_SignedIn (User As FirebaseUser)
    Log("#-Auth_SignedIn=" & User.DisplayName & ", User.PhotoUrl=" & User.PhotoUrl & ", User.Uid=" & User.Uid & ", User.Email=" & User.Email)
    Starter.strFirebaseSignedInUser = User.DisplayName
    Starter.strFirebaseSignedInUserEMail = User.Email
    Starter.strFirebaseSignedInUserID = User.Uid
    Starter.strFirebaseSignedInUserPictureUrl = User.PhotoUrl
   
    Starter.auth.GetUserTokenId(Starter.auth.CurrentUser, False )           
End Sub

Sub auth_TokenAvailable (User As FirebaseUser, Success As Boolean, TokenId As String)
Log("#-auth_TokenAvailable, Success=" & Success ) '& ", TokenId=" & TokenId)
    If Success Then
        Starter.strFirebaseSignedInUserToken = TokenId
        Log("TokenId=" & TokenId)
    Else
        Starter.strFirebaseSignedInUserToken = ""
    End If
End Sub

Sub Auth_SignOut(bolFollowedByNewSignIn As Boolean)
Log("#-Auth_SignOut")   
    Starter.auth.SignOutFromGoogle
    Starter.strFirebaseSignedInUser = ""
    Starter.strFirebaseSignedInUserEMail = ""
    Starter.strFirebaseSignedInUserPictureUrl = ""
    '
    If bolFollowedByNewSignIn Then
        Starter.auth.SignInWithGoogle
    End If
    '
End Sub   

Sub Button2_Click

    Log("b2 click")
    Activity.Finish
    StartActivity("Main")
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I would want to call a few labels of the backend on load of an activity, how can i do that, right now i am able to do it via click of a button, but i want to improve this to loading as soon the page is loaded
I haven't checked your code, however why can't you call the same code from Activity_Create?
 
Upvote 0
Top