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.
 

walterf25

Expert
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.
It is recommended that you post if not all your code, at least the relevant portions of the code you have so far, this helps us get an idea of what you're trying to do and be able to help you.

Reading the variables from a file is fairly easy, depending on the format you are using when storing the variables in the file, you can read them either with
B4X:
Dim m as Map
m = File.ReadMap(File.DirInternal, "settings.INI")
You will end up with a map object where you can then iterate through each key and value and assign the values read into the EditText.

Or you can read the entire file using
B4X:
Dim s As String
s = File.ReadString(File.DirInternal, "settings.INI")
Then you will have to parse the entire string and extract only the portions that you require.

There are many other ways you can do this, again it all depends on how you are storing the variables in the file.
To Store the values back into the same file you would again, read the values of each EditText and assign them to a map object.
B4X:
Dim m2 As Map
m2.Initialize
m2.Put("EditText1", EditText1.Text)
m2.Put("EditText2", EditText2.Text)
m2.Put(....
Then you would just write the contents of the Map object back to the settings.ini file.
B4X:
File.WriteMap(File.DirInternal, "settings.INI", m2)

This should get you gong.

Regards,
Walter
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
you need Initialize for a class, its in C# the new key word.
if you load a layout all the ui things (controls) are initialized.

look at
https://www.b4x.com/android/help/files.html
File.Exists
File.ReadMap
File.WriteMap
File.Delete

Map
https://www.b4x.com/android/help/collections.html#map

Type(Struct)
https://www.b4x.com/android/forum/pages/results/?query=type&product=b4a

You can pass data also via CallSubDelay... you need a public Sub

Activity Create comes if the new form open
Activity Pause & Resume if the form get hidden or appear again, Resume comes also after Create
put Log("your information") into a Sub for a better understanding

not all file extensions are allowed at android.
 
Last edited:
Upvote 0

ovt001

Member
Licensed User
Longtime User
Hi Walter,
thank you for you answer.
I found the map methode and what you tell me is what I did.

Dim m as Map
m = File.ReadMap(File.DirInternal, "settings.INI")

But my problem is more with the Edit.text hiself. Why did i have to "initialize" a Edit.text field?
I understand that variables have to be initialize but why a Edit.text? if i compare with Visual Basic, the text field are (per definitiion) a string....

Can you short explain me the role of Activity Resume?

thank you
O.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
But my problem is more with the Edit.text hiself. Why did i have to "initialize" a Edit.text field?
if u used
Activity.LoadLayout("Layout1") in Sub Activity_Create and Edit was part of this Layoutfile and at top in source code you see in Sub Globals
Private Edit As EditText or typical Private EditText1 As EditText (you can add it from the designer).
u can use it direct. but be sure what you read is valid and not null.

try to use the debug mode and break points.
 
Upvote 0

ovt001

Member
Licensed User
Longtime User
Ok, thank you
a last one for tonight:

When I search on my smartphone the "settings.ini" files, I dont find it.. But File.Exists(DirInternal, "Settings.ini") return True, and I can read the item with get.
Where is the file stored?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
[Phone]\android\data\b4a.example\Files
[phone]\Android\data\[your apps packagename]
you app is using ba.example as packagename. So the path is correct.

You are NOT using your own packagename which is mandatory to do for each app to setup a unique packagename.
 
Upvote 0

ovt001

Member
Licensed User
Longtime User
Hi Walter,
Ok let me tell you If I have good understand.
Map is a kind of "Array" where I can store the entry of a File. Correct?

If I want to read the full file I use:
B4X:
Dim m as Map
m = File.ReadMap(File.DirInternal, "settings.INI")

then I can read each line of the file with :
B4X:
Dim m as Map
m.Initialize
m.Get("Param1", EditText1.Text)
m.Get("Param2", EditText2.Text)
...

On the same way, If I want to write Settings into the file, I need first to fill in the Map with
B4X:
m.Get("Param1", EditText1.Text)
m.Get("Param2", EditText2.Text)
...

then I write then full map into the File wth:

B4X:
File.WriteMap(File.DirInternal, "settings.INI", m2)

Correct?

Can I add the "Settings.ini" file into the File Manager? how does has to look like?
Param1=value1
Param2=value2
...

Thank you
O.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
then I can read each line of the file with :
B4X:
Dim m as Map
m.Initialize
m.Get("Param1", EditText1.Text)
m.Get("Param2", EditText2.Text)
...
To read each field you only need to do the following...
B4X:
Dim m as Map
m.Initialize
EditText1.Text = m.Get("Param1")
EditText2.Text = m.Get("Param2")

On the same way, If I want to write Settings into the file, I need first to fill in the Map with
B4X:
m.Put("Param1", EditText1.Text)
m.Put("Param2", EditText2.Text)
...
To write the values to the settings.ini file this is the correct way.

Regards,
Walter
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
i guess you meant put as key value pair.

the assets folder is read only, you need to copy the settings template first into a folder with read/write access.

you can easy create the file in B4J at pc.
 
Last edited:
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
On the same way, If I want to write Settings into the file, I need first to fill in the Map with
B4X:
m.Get("Param1", EditText1.Text)
m.Get("Param2", EditText2.Text)
...

then I write then full map into the File wth:

B4X:
File.WriteMap(File.DirInternal, "settings.INI", m2)

Correct?

Can I add the "Settings.ini" file into the File Manager? how does has to look like?
Param1=value1
Param2=value2
...

Thank you
O.

Errr - no. You need to use Put to fill the map:

B4X:
m.Put("Param1", EditText1.Text)
m.Put("Param2", EditText2.Text)

File.WriteMap(File.DirInternal, "settings.INI", m)

- Colin.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Upvote 0

ovt001

Member
Licensed User
Longtime User
Oh, yes, sorry, I was wrong, of course I need to PUT to fill the map.
I made some small test program only to "play" with File.DirAsset en Map.

I know that the DirAsset is not accessible. I copy at the start of my program the file "Settings.ini" from DiAsset to DirInternal --> it's work.
I create a Settings.ini file and add it in the File Manager

The "Settings.ini" file contains 2 lines:
Var1=AAA
Var2=BBB
When I read the files, it returns wel:
AAA
BBB

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")
    File.WriteMap(File.DirInternal, "Settings.ini", Map1

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

Computersmith64

Well-Known Member
Licensed User
Longtime User
Oh, yes, sorry, I was wrong, of course I need to PUT to fill the map.
I made some small test program only to "play" with File.DirAsset en Map.

I know that the DirAsset is not accessible. I copy at the start of my program the file "Settings.ini" from DiAsset to DirInternal --> it's work.
I create a Settings.ini file and add it in the File Manager

The "Settings.ini" file contains 2 lines:
Var1=AAA
Var2=BBB
When I read the files, it returns wel:
AAA
BBB

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")
    File.WriteMap(File.DirInternal, "Settings.ini", Map1

This is working fine, but Var2 return "Null".
CCC
Null
How is this possible?
Thanks
Oli
Because the file is overwritten each time with whatever is in your map. So if you only put 1 parameter in your map, then that's all that will be written to the file.

- Colin.
 
Upvote 0
Top