Android Question Setting/Getting Arrays with StateManager

cyiwin

Active Member
Licensed User
Longtime User
I've been trying to save my variables using StateManager. I was doing fine until I got to my arrays. I have some arrays in multiples of 1000 and that would be a lot of code if I entered them line by line. I have been doing some reading but haven't had any luck doing this the short way. I want to make sure what I'm doing is possible, can someone tell me if I'm understanding how to do this correctly? I think I need to :
  1. Convert the Array to a List
  2. Use StateManager.SetSetting to set the List as a string
  3. StateManager.SaveSettings to save
  4. Use StateManager.GetSetting to load the List
  5. Convert the list back into its Array form
I wanted to make sure I was on the right track before posting some example code. Does it look like I am?

Thanks,
Brent
 

cyiwin

Active Member
Licensed User
Longtime User
Thanks for KVS, you're right, much simpler. I spent quite a while converting arrays to lists then saving data but I guess I didn't need to. This seems to work fine for regular arrays:

B4X:
Private KVS As KeyValueStore
Dim SomeArray(1000) as Int

KVS.Initialize(File.DirDefaultExternal, "DataStore")

'Put data into "DataStore"
KVS.PutObject("SomeArray", SomeArray)

'Get data from "DataStore"
KVS.GetObject("SomeArray")


Most of my arrays are in Type format, how do I do this with Types?

B4X:
Private KVS As KeyValueStore
Type Ship(Hps as int, Used as Boolean, Location as Int)
Dim SF(1000) as Ship

KVS.Initialize(File.DirDefaultExternal, "DataStore")

'Put data into "DataStore"
KVS.PutObject("SF", SF)    'Doesn't seem to work

'Get data from "DataStore"
KVS.GetObject("SF")      'Also probably wrong....
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
StateManager has two features:
1. Saving the state of the views (EditText text, selected Tab, CheckBox state...).
2. A simple way to save simple settings.

The main feature is the first and it is not related to KeyValueStore.

About the second one. It depends on your needs. If you need to store complex types then you should use KeyValueStore. There is no problem with using both modules.
 
Upvote 0
Top