Android Question Trying to Save a List of Maps to CSV

Tim Chapman

Active Member
Licensed User
Longtime User
I use this code:
B4X:
Sub SaveListOfMapsToCSV(FileDir As String, FileName As String, ListOfMaps As List, HeaderList As List)
    Dim su As StringUtils
    Dim ListOfArrays As List : ListOfArrays.Initialize
  
    'Put HeaderList at the top of the List to be saved.
    ListOfArrays.Add(HeaderList)

    'Walk Through List of Maps.
    'Get each Map from the list.
    'Get each Item from the Row.
  
    For Each item As Map In ListOfMaps
        Dim row(HeaderList.Size) As String
        For c = 0 To HeaderList.Size - 1
            row(c) = item.Get(HeaderList.Get(c))
            Log("row("&c&") = " & row(c))
        Next
        ListOfArrays.Add(row)
    Next
    File.Copy(FileDir, FileName, FileDir, FileName & "Old")
    File.Delete(FileDir, FileName)
    su.SaveCSV(File.DirRootExternal, FileName , "," , ListOfArrays)
    Msgbox(FileName, "File Saved")
End Sub

This is the log result:
row(0) = Purpose
row(1) = Amount
row(2) = Coin
row(3) = Source
row(4) = Date
row(5) = Location
row(6) = In/Out
row(0) = Borrowed to buy bow
row(1) = 100
row(2) = gp
row(3) = Arthuria
row(4) = 9/4/4/
row(5) = White City
row(6) = In
row(0) = Gift
row(1) = 2
row(2) = gp
row(3) = Desirra
row(4) = 9/4/6/
row(5) = White City
row(6) = In
row(0) = earned
row(1) = 2
row(2) = gp

main_savelistofmapstocsv (B4A line: 656)
su.SaveCSV(File.DirRootExternal, FileName , "," ,
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String[]
at anywheresoftware.b4a.objects.StringUtils.SaveCSV2(StringUtils.java:107)
at anywheresoftware.b4a.objects.StringUtils.SaveCSV(StringUtils.java:100)
at uk.co.martinpearman.tabhostextrasdemo.main._savelistofmapstocsv(main.java:2329)
at uk.co.martinpearman.tabhostextrasdemo.main._savecharacter_click(main.java:2231)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at anywheresoftware.b4a.BA$1.run(BA.java:303)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I can't see what is causing this error. Thanks in advance for assistance.
 
Last edited by a moderator:

Tim Chapman

Active Member
Licensed User
Longtime User
Thanks once again Erel. That worked.

For reference here is the code that works:
B4X:
Sub SaveListOfMapsToCSV(FileDir As String, FileName As String, ListOfMaps As List, HeaderList As List)
    Dim su As StringUtils
    Dim ListOfArrays As List : ListOfArrays.Initialize
   
    'Walk Through List of Maps.
    'Get each Map from the list.
    'Get each Item from the Row.
   
    For Each item As Map In ListOfMaps
        Dim row(HeaderList.Size) As String
        For c = 0 To HeaderList.Size - 1
            row(c) = item.Get(HeaderList.Get(c))
            Log("row("&c&") = " & row(c))
        Next
        ListOfArrays.Add(row)
    Next
    File.Copy(FileDir, FileName, FileDir, FileName & "Old")
    File.Delete(FileDir, FileName)
    su.SaveCSV2(File.DirRootExternal, FileName , "," , ListOfArrays, HeaderList)
    Msgbox(FileName, "File Saved")
End Sub
 
Upvote 0
Top