Android Question Weird behavior in File.ReadList

DTsEMT

Member
Licensed User
Longtime User
Any and all ideas are certainly appreciated!

I define a type and a list in Sub Globals

B4X:
    Type MyDrugs(DrugName As String, Amount As Int, Measure As Int, Volume As Int, AdminMeasure As Int)

    Dim SavedDrugs As List

Later in the code the user can define and enter the values for a new drug, which adds them to the list file:

B4X:
        Dim thisdrug As MyDrugs
        thisdrug.Initialize
        thisdrug.DrugName = ID.Input
        thisdrug.Amount = edtDrugAmt.Text
        thisdrug.Measure = spnDrugAmt.SelectedIndex
        thisdrug.Volume = edtVolSol.Text
        thisdrug.AdminMeasure = spnDoseOrd.SelectedIndex

        SavedDrugs.Add(thisdrug)
        File.WriteList(File.DirInternal, "saveddrugs.lst", SavedDrugs)

That seems to work well. On the next run of the app, the file "saveddrugs.lst" is read and a list of previously saved drugs is populated, HOWEVER the MyDrugs type has been split into several entries instead of one per line!

B4X:
    <activity create>
    
    SavedDrugs.Initialize
    If File.Exists(File.DirInternal,"saveddrugs.lst") Then
        SavedDrugs = File.ReadList(File.DirInternal,"saveddrugs.lst")
        For i = 0 To SavedDrugs.Size-1
            Log(i & ", " & SavedDrugs.Get(i))
        Next

log:

** Activity (main) Create, isFirst = true **
0, [AdminMeasure=0, Amount=1, DrugName=DRUG1
1, , IsInitialized=true, Measure=0, Volume=1
2, ]

Which is causing all kinds of errors. I've checked the DrugName variable to certify there are no CrLF characters, and even renamed it to AAADrugname, which places it first in the list.

Every time I store the list, only three items make it into the first line, three into the second, etc. instead of each MyDrugs type occupying a single line.

Is this normal behavior for the List type?
 

agraham

Expert
Licensed User
Longtime User
I think from the documentation that ReadList and WriteList are intended for use with text files where the List items are strings. There is certainly no intelligence in the code to cope with taking custom types apart to write them and re-instancing them when read. What you are seeing is the result of converting them to a readable string.

In general File deals with strings and RandomAccessFile deals with objects. I haven't ever used them but RandomAccessFile.WriteB4XObject and ReadB4XObject may do what you need. The documentation is not entirely clear if Lists of user defined types are supported but it looks like individual ones may be. Internally they use B4XSerializator.
WriteB4XObject

Description
Similar to WriteObject. This method writes the object in a format supported by B4i, B4A and B4J.
The following types are supported: Lists, Arrays of bytes and Arrays of objects, Maps, Strings, primitive types and user defined types.
Note that user defined types should be declared in the Main module.

ReadB4XObject

Description
Reads an object previously written with WriteB4XObject.
 
Upvote 0

DTsEMT

Member
Licensed User
Longtime User
I think from the documentation that ReadList and WriteList are intended for use with text files where the List items are strings. There is certainly no intelligence in the code to cope with taking custom types apart to write them and re-instancing them when read. What you are seeing is the result of converting them to a readable string.

In general File deals with strings and RandomAccessFile deals with objects. I haven't ever used them but RandomAccessFile.WriteB4XObject and ReadB4XObject may do what you need. The documentation is not entirely clear if Lists of user defined types are supported but it looks like individual ones may be. Internally they use B4XSerializator.

Thank you, I'll give it a try!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top