Android Question Analising a saved list

saunwin

Active Member
Licensed User
Longtime User
HI Guys,

I'm saving my list.
Lists are saved as text files using commas to delimit the fields - That's Great
However in the saved file, theres a CRLF after name and an isinitialized field.
There is no CRLF - in fact I (think) I strip it with nameinput.Text = nameinput.Text.replace(CRLF, "") so each entry is on one line
and what is the Isinitialized field ? - Can i get rid of it ?

TIA
Steve

[IsInitialized=false, email=[email]shagger@theunwins.com[/email], name=shaggernumber2
, score=7, smoke=true]
[IsInitialized=false, email=[email]shagger@theunwins.com[/email], name=joe
, score=5, smoke=true]
[IsInitialized=false, email=[email]shagger@theunwins.com[/email], name=shagger
, score=1, smoke=true]
 

stevel05

Expert
Licensed User
Longtime User
It looks like you are saving a type as an object, The Isinitialized field is part of the object.

To save the text from the objects you need to do something like:

B4X:
Dim SB as StringBuilder
SB.initialize

For Each T as TypeName In L  'Where L is your list and TypeName is whatever you called the Type
    SB.Append(T.Name).Append(",").Append(T.Email).Append(","),Append(T.Score).Append(",").Append(T.Smoke).Append(CRLF)
Next
File.WriteString(Filedir,FileName,SB.ToString)
 
Upvote 0

saunwin

Active Member
Licensed User
Longtime User
Thanks Steve

Not all fields are a string, although I guess they probably could be.


Type person (name As String, email As String, smoke As Boolean, score As Int)

B4X:
Dim p As person
            p.Name=nameinput.Text
            p.email=emailinput.Text
            p.score=Rnd(0,10)
            p.smoke=anontick.Checked
            persons.Add(p)
            scores.Clear
            persons.SortType("score",False)
            For i = 0 To persons.Size-1
                Dim p As person
                    p=persons.Get(i)
                    Log(p)
            Next   
            
            Dim x As Int
            x = persons.Size-1
            If x >10 Then x=10
            
            For i=0 To x
                    p=persons.Get(i)
                    scores.AddSingleLine(p.name&"    "&p.score)
            Next
            
            Log(persons)

            File.WriteList (File.DirDefaultExternal, filename , persons)
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Int won't be a problem, what does it save Smoke As? Probaly "true" or "false". if you want to read it back into an app, you'll need to convert it back to Boolean.

B4X:
Dim Smoke as Boolean = "true" = Val
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Since you use a custom type and have an element that is boolean, another way would be to use B4XSerializator to convert the list of custom type to bytes, then read the bytes and convert the bytes via the serializator to a list and extract your data. But Steve's answer may be good enough for what you are doing. Please correct Analising to Analyzing in your title
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thanks Steve - I figured it out using your suggestion
Can you share the pertinent code on how you ended up doing it so I can compare it to the way I would do which is using B4XSerializator. Maybe, it is better than the way I do it.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I suggested that solution as I assumed that Steve wanted to read or use the csv file created.

If you are only reading the file back within an app, and particularly if there is more data, it would be better to use B4XSerializator. If will take up less space as it compresses the data, and will not be saved in human readable form.
 
Last edited:
Upvote 1
Top