Android Question How do I save a list of maps to a CSV?

Tim Chapman

Active Member
Licensed User
Longtime User
I load the following csv file:

"Skill Name","Total","Mod","Key","Ranks","Class","CC","Trained","SP","Misc","Synergy","TCP"
"Left","Center","Center","Center","Center","Center","Center","Center","Center","Center","Center","Center"
"Appraise",6,3,"Int",0,,true,false,0,3,0,0
"Balance",-1,2,"Dex",0,,true,false,0,0,0,-3
"Bluff",1,1,"Cha",0,,true,false,0,0,0,0
"Climb (with climb kit)",2,2,"Str",1,"F,A",false,false,1,2,0,-3

Then convert it to a list of maps with this code:

B4X:
Sub LoadCSVFromFile(FileDir As String, FileName As String, ListOfMaps As List, HeaderList As List, Justification As Map)
    Dim ListFromFile As List
    Dim ReturnMap As Map : ReturnMap.Initialize()
    Dim cells() As String
    Dim su As StringUtils
  
    'Load Skill List from file
    ListFromFile.Initialize
    ListFromFile = su.LoadCSV2(FileDir, FileName, ",", HeaderList)
    Log("ListFromFile = " & ListFromFile)      
  
    'Populate Justification Map from 1st row in ListFromFile.  Note that the header row is not counted because the HeaderList took that from the count.
    Justification.Initialize()
    cells = ListFromFile.Get(0)
    For j = 0 To HeaderList.Size -1
        Justification.Put(HeaderList.Get(j), cells(j))
    Next

    'Populate List of Maps (one map for each skill, spell, etc.)
    For i = 1 To ListFromFile.Size - 1
        cells = ListFromFile.Get(i)
        Dim ItemMap As Map
        ItemMap.Initialize()
        For j = 0 To HeaderList.Size - 1
            ItemMap.Put(HeaderList.Get(j), cells(j))
        Next
        ListOfMaps.Add(ItemMap)
    Next
End Sub

I want to convert the list of maps back to the original csv file format and save it. I have been trying for hours to get that done with no success. How can I do this?
Thanks again for assistance!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. No need to initialize ListFromFile as you are assigning it a different list anyway.

2. Convert the list of maps to a list of arrays:
B4X:
Dim listOfArrays As List
listOfArrays.Initialize
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))
 Next
 listOfArrays.Add(row)
Next
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
Thanks Erel,
I am trying to save it back to a csv file like the original.
I tried to save a list of arrays to a csv file and can't do so. They can't be cast as string. I get a java exception to that effect when I try to do so.
What should I do to save it back to a csv file like the original?
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
I ran the code you supplied with my addition of the su command at the bottom to save the CSV file.
B4X:
    Dim listOfArrays As List
    listOfArrays.Initialize
    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))
        Next
        listOfArrays.Add(row)
    Next
    su.SaveCSV(File.DirRootExternal, "NewSaved" & FileName , "," , NewList)

Here is the log results:
main_loadcsvfromfile (B4A line: 366)
su.SaveCSV(File.DirRootExternal, "NewSaved" & Fil
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._loadcsvfromfile(main.java:1225)
at uk.co.martinpearman.tabhostextrasdemo.main._activity_create(main.java:408)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:179)
at uk.co.martinpearman.tabhostextrasdemo.main.afterFirstLayout(main.java:102)
at uk.co.martinpearman.tabhostextrasdemo.main.access$000(main.java:17)
at uk.co.martinpearman.tabhostextrasdemo.main$WaitForLayout.run(main.java:80)
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)
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
Next Question:
Is there a way to keep the order of the saved csv list the same as the original order I loaded from the original CSV file?
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
I just realized that it is not in a different order. It is just that the first item is missing. I hope I can fix that.
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
Thanks so much for your assistance! I am a new programmer. The majority of the code I posted came from my brother who is a 30 year veteran of python and databases. I am wrapping my head around how this stuff works. I will analyze what you gave me and figure out how it works.
Thanks again for your assistance!
 
Upvote 0
Top