Android Question Casting with structures (type) and lists

mdehrnsb

Member
Licensed User
Longtime User
I am trying to create an array of structures (types) and then save them to a list. I then want to write the list to a file using File.WriteList(). These appears to work but when I try to read the list back in, I an getting an exception. I believe the problem is casting but I have tried several solutions but none successful.

The structure is defined in Process_Globals as:

Type RVStruct(Name As String, Length As Int, Width As Int)



Here is the code snippet. The first section created the file. The second reads it. Any assistance would be appreciated

If File.Exists(File.DirDefaultExternal, "ModelsList.txt") = False Then


Dim RVModels As List

RVModels.Initialize

Dim RVM(3) As RVStruct

RVM(0).Initialize
RVM(0).Name = "ModA"
RVM(0).Length = 185
RVM(0).Width = 96

RVM(1).Initialize
RVM(1).Name = "ModB"
RVM(1).Length = 225
RVM(1).Width = 95

RVM(2).Initialize
RVM(2).Name = "ModC"
RVM(2).Length = 250
RVM(2).Width = 94

RVModels.AddAll(RVM)


File.WriteList(File.DirDefaultExternal, "ModelsList.txt", RVModels)


Else

Dim RVModels As List
Dim XM As RVStruct



RVModels.Initialize
XM.Initialize


RVModels = File.ReadList(File.DirDefaultExternal, "ModelsList.txt")

XM = RVModels.Get(0)

Log("Name = " & XM.Name)
Log("Length = " & XM.Length)
Log("Width = " & XM.Width)



End If
 

DonManfred

Expert
Licensed User
Longtime User
1.
Please use [CODE]code here...[/CODE] tags when posting code.

codetag001.png

codetag002.png

codetag003.png

2. Check the Docs

File. WriteList (Dir As String, FileName As String, List As List)
Writes each item in the list as a single line.
Note that a value containing CRLF will be saved as two lines (which will return two item when read with ReadList).
All values will be converted to strings.
3. Use B4XSerialisator to write/read the list
 
Upvote 0

derez

Expert
Licensed User
Longtime User
"File" needs the objects in the list to be primitives, not types. You cam overcome this limitation by saving the content of the type as a string, like this code (b4j for my convenience):
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim RVModels As List
    Type RVstruct (Name As String,Length As Int, Width As Int)
    Dim RVM(3) As RVstruct
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
'    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
'    MainForm.Show

    If File.Exists(File.DirApp, "ModelsList.txt") = False Then
        RVModels.Initialize
        RVM(0).Initialize
        RVM(0).Name = "ModA"
        RVM(0).Length = 185
        RVM(0).Width = 96
   
        RVM(1).Initialize
        RVM(1).Name = "ModB"
        RVM(1).Length = 225
        RVM(1).Width = 95

        RVM(2).Initialize
        RVM(2).Name = "ModC"
        RVM(2).Length = 250
        RVM(2).Width = 94
       
        For i = 0 To 2
            RVModels.Add(RVM(i).Name &","& RVM(i).Length &","&    RVM(i).Width)
        Next
        File.WriteList(File.DirApp, "ModelsList.txt", RVModels)
    Else

        Dim XM As RVstruct
        Dim str() As String
        RVModels.Initialize
        XM.Initialize
        RVModels = File.ReadList(File.DirApp, "ModelsList.txt")
       
        str = Regex.Split(",",RVModels.Get(1))
        XM.Name = str(0)
        XM.Length = str(1)
        XM.Width = str(2)

        Log("Name = " & XM.Name)
        Log("Length = " & XM.Length)
        Log("Width = " & XM.Width)
    End If
End Sub

Notes: 1. when you publish code - use the "insert code" in the menu above the post.
 
Last edited:
Upvote 0
Top