Android Question Initialize fields and some basic questions

ovt001

Member
Licensed User
Longtime User
Hello everybody.
First I want to apologize for the stupid question of this post, but I start (again) with B4A and I think that I have still to learn some basic functions.
Here my questions:
I made some small apps with 2 activities ("main" and "Settings").

The goal of this mini app is only to learn how I can :
1- read variable from an INI file (store into Dir.internal)
2- store those variable into Edit Text on the "Settings" activity
3- Write the variables from the "Settings" Activity into the same INI File.

Like you see, nothing very exiting :)

I already write some code, but I don't understand the principe of "Initialize" variables. (the log inform me that i have to initialize Edit.text even when I initialize it....
I want also to know the difference between "Activity Create" and "Activity Resume" when i want to pass variable between 2 activities (Process_Global)

Thank you for uour assistance.
Regards
O.
 

ovt001

Member
Licensed User
Longtime User
Hi Colin
I have then to fill all parameters of the map before I write it. It makes sense :)
Thank you for your answer.

cheers
O.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi Colin
I have then to fill all parameters of the map before I write it. It makes sense :)
Thank you for your answer.

cheers
O.

Yes. Another way to do it is to create a "settings" class that holds the map & takes care of the read / write functions, then create reference to the class in the Process_Globals of your Starter service. Then you can access it from your other activities, read it once, make changes & write it back. Something like this:

B4X:
'Class module "clsSettings"
Sub Class_Globals
    Private mDir As String
    Private mSettingsFile As String  
    Private mSettings As Map
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(settingsDir as String, settingsFile as String)
  
    mDir = settingsDir
    mSettingsFile = settingsFile   
    mSettings.Initialize
  
    mSettings.Put("Param1", "Default Value 1")
    mSettings.Put("Param2", "Default Value 2")
End Sub

Public Sub setParameter1(val as String)
    mSettings.Put("Param1", val)
End Sub

Public Sub getParameter1() As String
    Return mSettings.Get("Param1")
End Sub

Public Sub setParameter2(val as String)
    mSettings.Put("Param2", val)
End Sub

Public Sub getParameter2() As String
    Return mSettings.Get("Param2")
End Sub

Public Sub readSettings
     
    If File.Exists(mDir, mSettingsFile) Then
        mSettings= File.ReadMap2(mDir, mSettingsFile, mSettings)
    End If
  
End Sub

Public Sub writeSettings
    File.WriteMap(mDir, mSettingsFile, mSettings)
End Sub
'==================================================

'Starter Service
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
  
    Public cSettings As clsSettings  
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    cSettings.Initialize(File.DirInternal, "settings.ini")
 
End Sub
'=====================================================

'Main Activity
Sub Activity_Create(FirstTime As Boolean)
  
    [...]
  
    Starter.cSettings.readSettings
    Log(Starter.cSettings.Parameter1)
    Starter.cSettings.Parameter1 = "New Value"
    Starter.cSettings.writeSettings

End Sub

Doing this means you only ever have 1 instance of your settings class, & your settings map is always up to date with the correct values. As long as you remember to call Starter.cSettings.writeSettings every time you make a change to the map, your persistent copy of the settings in your settings.ini will always be up to date as well - so that the next time the app starts & you call Starter.cSettings.readSettings, you will get the most recent copy of saved settings.

- Colin.
 
Upvote 0

ovt001

Member
Licensed User
Longtime User
Hi Colin,
I will check the Class later (when I will be more familiarly with the basic functions :))

I have another issue with EditText.
I put a EditText view on an Activity. Name of the EditText is "txtNameCam1" then value is "aaaaa"
I write it into the Settings.ini file with this code
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 txtNameCam1 As EditText
 End Sub

Dim SettingsMap1 As Map
    SettingsMap1.Initialize
    SettingsMap1.Put("NameCam1",txtNameCam1)
    File.WriteMap(File.DirInternal, "Settings.ini", SettingsMap1)

when I read the value again, NameCam1 value is: "android.widget.EditText{104288f VFED..CL. ........ 296,106-677,233 #3}" and not "aaaaa"

I think that I have to initialize EditText, but I am not sure.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi Colin,
I will check the Class later (when I will be more familiarly with the basic functions :))

I have another issue with EditText.
I put a EditText view on an Activity. Name of the EditText is "txtNameCam1" then value is "aaaaa"
I write it into the Settings.ini file with this code
B4X:
Dim SettingsMap1 As Map
    SettingsMap1.Initialize
    SettingsMap1.Put("NameCam1",txtNameCam1)
    File.WriteMap(File.DirInternal, "Settings.ini", SettingsMap1)

when I read the value again, NameCam1 value is: "android.widget.EditText{104288f VFED..CL. ........ 296,106-677,233 #3}" and not "aaaaa"

I think that I have to initialize EditText, but I am not sure.

If you just want to save the value of the EditText you need to reference the .Text property:

B4X:
Dim SettingsMap1 As Map
    SettingsMap1.Initialize
    SettingsMap1.Put("NameCam1",txtNameCam1.Text)
    File.WriteMap(File.DirInternal, "Settings.ini", SettingsMap1)

- Colin.
 
Upvote 0

ovt001

Member
Licensed User
Longtime User
@Colin, you a faster than me :)


I found my error. it have to be
B4X:
SettingsMap1.Put("NameCam1",txtNameCam1.text)

and not

B4X:
SettingsMap1.Put("NameCam1",txtNameCam1)

Sorry for this beginner question :)
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
But when I change the value of Var1 to "CCC", I use this code
B4X:
    Dim Map1 As Map
    Map1.Initialize
    Map1.Put("Var1","CCC")

This is working fine, but Var2 return "Null".
CCC
Null
How is this possible?
Thanks
Oli

the reason is you create a new Map1 object and put only one key,value pair in it.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Upvote 0
Top