Android Question Save Map To KeyValueStore, Load Map From KeyValueStore

Discussion in 'Android Questions' started by Pete_S, Aug 28, 2018.

  1. Pete_S

    Pete_S Member Licensed User

    I created this code. It works but I was wondering if there is an easier/optimized way to save a map to a KeyValueStore and retrieve it back from the KeyValueStore.

    Main:
    Code:
    Sub Process_Globals
    End Sub
    Sub Globals
    End Sub
    Sub Activity_Create(FirstTime As Boolean)
        
    Dim map1 As Map
        map1.Initialize
        map1.Put(
    1"click")
        map1.Put(
    2"clack")
        map1.Put(
    3"snap")
        map1.Put(
    4"slappy")
        SaveMapToKVS(Starter.kvs, map1, 
    "key_string_1""value_string_1")
        
    Dim map2 As Map
        map2.Initialize
        map2 = LoadMapFromKVS(Starter.kvs, 
    "key_string_1""value_string_1")
        
    Log("Test to see if it works:")
        
    For i = 0 To map2.Size - 1
            
    Log(map2.GetKeyAt(i) & " = " & map2.GetValueAt(i))
        
    Next
    End Sub
    Sub LoadMapFromKVS(kvs As KeyValueStore, keyname As String, valuename As StringAs Map
        
    Dim map_new As Map
        map_new.Initialize
        
    Dim StringArray() As String
        
    Dim StringArray2() As String
        
    Dim s As String
        
    Dim s2 As String
        s = Starter.kvs.Get(keyname)
        s2 = Starter.kvs.Get(valuename)
        StringArray = 
    Regex.Split(",", s)
        StringArray2 = 
    Regex.Split(",", s2)
        
    For i = 0 To StringArray.Length - 1
            map_new.Put(StringArray(i), StringArray2(i)) 
    'CInt(StringArray(i)) ?
        Next
        
    Return map_new
    End Sub
    Sub RemoveLastCharacter(s As StringAs String
        
    Return s.SubString2(0, s.Length - 1)
    End Sub
    Sub SaveMapToKVS(kvs As KeyValueStore, map1 As Map, keyname As String, valuename As String)
        
    Dim s As String
        
    For Each v As Int In map1.keys
            s = s & v & 
    ","
        
    Next
        s = RemoveLastCharacter(s)
        Starter.kvs.Put(keyname, s)
        s = 
    ""
        
    For Each v2 As String In map1.values
            s = s & v2 & 
    ","
        
    Next
        s = RemoveLastCharacter(s)
        Starter.kvs.Put(valuename, s)
    End Sub
    Starter:
    Code:
    Sub Process_Globals
        
    Public kvs As KeyValueStore
    End Sub
    Sub Service_Create
        kvs.Initialize(
    File.DirInternal, "datastore2")
    End Sub
     
    Last edited: Aug 29, 2018
  2. Mashiane

    Mashiane Expert Licensed User

    Perhaps..

    Code:
    Sub Map2Json(m As MapAs String
        
    Dim gen As JSONGenerator
        
    Dim outJSON As String
        gen.Initialize(m)
        outJSON = gen.ToString
        
    Return outJSON
    End Sub
    and

    Code:
    Sub Json2Map(jsonText As StringAs Map
        
    Dim json As JSONParser
        
    Dim Map1 As Map
        Map1.Initialize
        
    Try
            
    If jsonText.length > 0 Then
                json.Initialize(jsonText)
                Map1 = json.NextObject
            
    End If
            
    Return Map1
        
    Catch
            
    Return Map1
        
    End Try
    End Sub
    then you can save and retrive the json string using a key you specify.
     
    BillMeyer and joulongleu like this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    @Pete_S there are several mistakes in your code and it is completely not required.

    Yes.

    Code:
    KVS.Put("my map", map1)
    Dim m As Map = KVS.Get("my map")
     
    AnandGupta likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice