Android Question store 4 variables in persistent (flash memory)

fgh3966

Active Member
Licensed User
Hello all
I would like to store some variables in persistent memory with the simplest code to understand and also the shortest possible. I saw kvs2 and I'm looking for an easy to understand example.

Thank.
 

fgh3966

Active Member
Licensed User
Thank you
for read variables this code is right ?

read variables with kvs:
kvs.get("a", a)
kvs.get("b", b)
kvs.get("c", c)
kvs.get("d", d)
 
Upvote 0

fgh3966

Active Member
Licensed User
Thank you for your replies.

When i wrote this code i obtain "javalangNullPointerExeption" in my phone
I try ramvar as string or int variable

save variable:
Private Sub btnSave_Click   
    
    kvs.put("ramvar",flashvar)
    LBL_rate1.text = "saved"& titi
    
End Sub
 
Upvote 0

fgh3966

Active Member
Licensed User
Good evening.
I would like to declare, initialize, the kvs2 library to store some variables.
I activated kvs2 and do some tests. But i'm not understand in which sub should we declare kvs2 and initialize it ?
I tried KeyValueStoreExample.zip , after renaming the activity, it compiles but doesn't install.
Is possible to have a simple example without sql ?
Also via this link saves the informations item by item ?

Thank you.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Is possible to have a simple example without sql ?
If you want someting that does not use SQL, then using a map is simplest. You can save a map to a file and read the file to retrieve variables.. Here is a full example if the values are strings:
B4X:
Dim a As String ="Paris"
    Dim b As String ="London"
    Dim c As String ="Athens"
    Dim d As String ="Geneva"

    Dim m As Map
    m.Initialize
    m.Put("a", a)
    m.Put("b", b)
    m.Put("c", c)
    m.Put("d", d)
   
    'save map:
    File.WriteMap( xui.DefaultFolder, "mycities", m)
   
    'Read the map
    Dim mp As Map =File.ReadMap(xui.DefaultFolder, "mycities")
    Log(mp.Get("c"))   'returns the value for the key c which is: Athens
 
Last edited:
Upvote 0

MikeH

Well-Known Member
Licensed User
Longtime User
Better to use:

B4X:
a = kvs.getdefault("a", someinitialvalue)

as "a" will likely be empty at the start.
 
Upvote 0
Top