Android Question AnotherDatePicker (B4X) error r/w to a Map

Discussion in 'Android Questions' started by LucaMs, Apr 18, 2018.

Tags:
  1. LucaMs

    LucaMs Expert Licensed User

    I'm using a Map to save a date (long) selected with AnotherDatePicker.

    When I read the map I get "strange" error (set a breakpoint on line 67 - Dim lngDate...).
     

    Attached Files:

  2. DonManfred

    DonManfred Expert Licensed User

    Code:
    Sub Read
        
    Log("Reading")
        
    If File.Exists(File.DirInternal, DATES_FILENAME) Then
            mmapDates = 
    File.ReadMap(File.DirInternal, DATES_FILENAME)
            
    Log(mmapDates)
            
    For Each key As String In mmapDates.Keys
                
    If key = DATES_FIRST_KEY Then
                    
    Dim lngDate As Long = mmapDates.Get(key)
                    AnotherDatePicker1.Date = lngDate
                
    End If
            
    Next
        
    End If
    End Sub
     
  3. LucaMs

    LucaMs Expert Licensed User

    Why? Map.Get does not work anymore?
     
  4. DonManfred

    DonManfred Expert Licensed User

    As you can see in my code map.Get does work if you use the right Key
     
  5. DonManfred

    DonManfred Expert Licensed User

    i suggest to use String as KEytype

    in this case
    Code:
    Private Const DATES_FIRST_KEY As String = "1"
    does work without the loop over all keys

    Code:
    Sub Read
        
    Log("Reading")
        
    If File.Exists(File.DirInternal, DATES_FILENAME) Then
            mmapDates = 
    File.ReadMap(File.DirInternal, DATES_FILENAME)
            
    Log(mmapDates)
            
    'For Each key As String In mmapDates.Keys
            '    If key = DATES_FIRST_KEY Then
            Dim lngDate As Long = mmapDates.Get(DATES_FIRST_KEY)
                    AnotherDatePicker1.Date = lngDate
                
    'End If
            'Next
        End If
    End Sub
     
  6. LucaMs

    LucaMs Expert Licensed User

    I have not tried but I trust.
    Anyway, you can see that the key I used is a constant of type Int.
     
  7. LucaMs

    LucaMs Expert Licensed User

  8. LucaMs

    LucaMs Expert Licensed User

  9. DonManfred

    DonManfred Expert Licensed User

    Yes. But as you are using File.WriteMap all Values are converted to Strings! So after File.ReadMap all Keys are Strings. You then try to access the Key using an Int and this leads into a null because the Key does not exists.

    https://www.b4x.com/android/forum/pages/results/?query=File.WriteMap

    The solution is to use KVS or raf.WriteB4XObject.
     
    LucaMs likes this.
  10. LucaMs

    LucaMs Expert Licensed User

    This was my first idea; then I thought: "Why use KVS since you can save maps?" :)

    Thank you.
     
  11. DonManfred

    DonManfred Expert Licensed User

    If you do it right then it does work. Use RandomAccessFiles WriteB4XObject/ReadB4XObject
     
    LucaMs likes this.
  12. LucaMs

    LucaMs Expert Licensed User

    Yes but I wanted to use an easy, quick way and with few libraries, since I don't think to complete this app.

    Initially I was thinking to save a simple string! :D
     
  13. LucaMs

    LucaMs Expert Licensed User

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