Android Question creating and editing a config file

Colin Evans

Active Member
Licensed User
Longtime User
Hi, just wondered if there were any examples of creating, saving and editing a configuration file, I've tried it with an Excel file, where I created the excel file manually, enter the data in the excel file and whilct I can display it on the config / settings screen I can't edit and then save the changes, or at least I can't find a way of doing it.

Am I better to use a database file or is there a simple way of reading and writing to a text file that I could use

Cheers, Colin
 

Colin Evans

Active Member
Licensed User
Longtime User
sorry but doesn't make any sense to me, all I want is a simple file that contains something like

location1 = Somewhere1
Code1 = code1
Location2 = Somewhere else
COde2 = another code
location3 = and yet another
Code 3 = and another code

I need to load them into a settings screen and be able to edit them and then save back to the file, like I say I could only think to use an excel file where I can read them into the screen okay and whilst I can then edit them I can't save back to the original file and overwrite and changes
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
I would if i could write back any changes made, which I don't believe you can do to an existing excel file
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User

I use a Map file.

All my apps have a map file, which I call ApVals.Txt, that I use to store a dozen or so parameters.

I use something like:
B4X:
Dim ApVals as Map

if (File.Exists(File.DirDefExternal, "ApVals.Txt") = False) Then
  Apvals.Initialize
  ApVals.Put("location1", "Somewhere1") ' create configuraiton parameters with default values
  ApVals.Put("Code1", 1234)
    ... and so on ...
Else
  Apvals = File.ReadMap(File.DirDefExternal, "ApVals.Txt")
End if

(Note: that code is off the top of my head - it may contain errors)

The ApVals.Txt file will be a text file with the format:

location1=Somewhere1
Code1=1234

During execution, if a configuration parameter changes, I .Put it to ApVals and write ApVals to disk (File.WriteMap). If I need to check a parameter's value, I .Get the value from ApVals.


Barry.
 
Last edited:
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User


Keyvaluestore2 is your best solution and it has very simple understandandable example, which accept map and stores values in SQLite db rather than text file.

https://www.b4x.com/android/forum/t...imple-powerful-local-datastore.63633/#content
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Thanks all, I'll have a go at the Keyvaluestore2 but thanks to Barry seems a good fall back as I'm not bothered about security so a text file is pretty good, thanks again
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Barry, thanks again for taking the time to show me your method, I couldn't be cheeky and ask how I'd read back into the config screen and then if any changes have been made, to save them back to the file, if this is possible, sorry to ask just getting to grips with B4A, cheers
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
I've tried this getting nowhere
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Settings")
    Dim ApVals As Map
    If (File.Exists(File.DirAssets, "ApVals.Txt") = False) Then
        ApVals.Initialize
        ApVals.Put("location1", "Rhyl") 
        ApVals.Put("cfgLong1", "53.324044")
        ApVals.Put("cfgLat1","-3.506209")
        ApVals.Put("location2","")
        ApVals.Put("Long2","")
        ApVals.Put("Lat2","")
        ApVals.Put("location3","")
        ApVals.Put("Long3","")
        ApVals.Put("Lat3","")
    Else
        ApVals = File.ReadMap(File.DirAssets, "ApVals.Txt")
        txtLocation1.Text=ApVals.Get("location1")
        cfgLong1.Text=ApVals.Get("long1")
        cnfLat1.Text=ApVals.Get("lat1")
        txtLocation2.Text=ApVals.Get("location2")
        cfgLong2.Text=ApVals.Get("long2")
        cfgLat2.Text=ApVals.Get("lat2")
        txtLocation3.Text=ApVals.Get("location3")
        cfgLong3.Text=ApVals.Get("long3")
        cfgLat3.Text=ApVals.Get("lat3")
    End If
End Sub

Sub btnSave_Click
    Dim ApVals As Map
    File.writeMap(File.DirAssets, "ApVals.Txt",ApVals)
        ApVals.Put("location1", txtLocation1.Text) '
        ApVals.Put("cfgLong1", cfgLong1.Text)
        ApVals.Put("cfgLat1", cnfLat1.Text)
        ApVals.Put("location2",txtLocation2.Text)
        ApVals.Put("Long2",cfgLong2.Text)
        ApVals.Put("Lat2",cfgLat2.Text)
        ApVals.Put("location3",txtLocation3)
        ApVals.Put("Long3",cfgLong3.Text)
        ApVals.Put("Lat3",cfgLat3.Text)
End Sub
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User

I think I understand your question correctly…

I have done apps that have "Configuration Screens" and also use the ApVals map method to store configuration parameters.

I just used a brute force approach:

When the configuration screen page is opened I populate each control by reading the value from ApVals and writing it to the control. For example suppose an EditText, edLoc1, allowed the value for location1 to be changed. When the configuration screen opened, I would have: edLoc1.Text = ApVals.Get(location1)

On the location screen I would have "OK" and "Cancel" buttons. If the user pressed the OK button, I would go through each control and write its current value back to the ApVals map and finally write Apvals back to the ApVals.Txt file. If the user pressed the Android Back button or Cancel button I would close the configuration screen without writing the changes back to ApVals (the user would lose any changes).

Hope this helps.

Barry.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Barry, again thanks for taking the time to reply, so could you explain where I'm going wrong, the first time I ran the program it filled in the first three fields as displayed in the code above and then displayed the results on my screen, but if I amend the three fields or add to the other locations and then click the button btnSave, the btnSave_Click should I thought update the results back to the file

But when I then go back into the program nothing is displayed, and if I enter anything and press the save button I still don't get anything returned, I assume its not saving correctly, help!
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
File.writeMap(File.DirAssets, "ApVals.Txt",ApVals)
Colin: You can not save a map or any file for that matter to the assets folder (File.DirAssets). It is a read only folder. You need to save it to File.DirInternal, file.DirdefaultExternal or file.DirRootexternal, etc.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi, I changed all the file.dirAssets to File.DirInternal but the program simply crashes when I try to save
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Hi, I changed all the file.dirAssets to File.DirInternal but the program simply crashes when I try to save

B4X:
Sub btnSave_Click
    Dim ApVals As Map
    ApVals.Initialize ' <------

    ApVals.Put("location1", txtLocation1.Text) '
    ApVals.Put("cfgLong1", cfgLong1.Text)
    ApVals.Put("cfgLat1", cnfLat1.Text)
    ApVals.Put("location2", txtLocation2.Text)
    ApVals.Put("Long2",cfgLong2.Text)
    ApVals.Put("Lat2", cfgLat2.Text)
    ApVals.Put("location3", txtLocation3)
    ApVals.Put("Long3", cfgLong3.Text)
    ApVals.Put("Lat3", cfgLat3.Text)

    File.writeMap(File.DirInternal, "ApVals.Txt", ApVals) ' <--- write to "disk" after setting key/value pairs
End Sub

I believe you need to initialize the map ApVals after it's declared.
I believe you want to set the values of ApVals before writing the map to "disk".

Barry.
 
Upvote 0

Colin Evans

Active Member
Licensed User
Longtime User
Hi Barry, thanks that seems to have solved it (nearly) appreciate you and the other members taking the time to help a novice, cheers
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…