Android Question Saving and loading data of a self defined type

Tjitte Dijkstra

Member
Licensed User
Longtime User
I try to save names and ages of people who play the little game that I develop.
I defined a type INI (see Process_Globals below) and defined INIval as a variable for 1 person and INIvalues as a list for a lot of persons.
In SAVE_Click the List is saved. So far so good, BUT
In LOAD_Click I get an error: ClassCastException:Java.Lang.String cannot be cast

How to avoid this?
TD NL 3843 XP
SOURCE CODE:

Sub Process_Globals
Type INI (FamilyName As String, Age As Int)
End Sub

Sub Globals
Dim INIval As INI
Dim INIvalues As List

Private LOAD As Button 'to retrieve data from file
Private Naam As EditText 'to get the familyname
Private RESULT As Label 'to present the retrieved data
Private SAVE As Button 'to save the list
Private Age As EditText 'to get the age
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main") 'you can image what it looks like?
INIval.Initialize()
INIvalues.Initialize()
End Sub

Sub Naam_EnterPressed
INIval.FamilyName = Naam.Text
End Sub

Sub Age_EnterPressed
INIval.Age = Age.text
End Sub

Sub SAVE_Click
INIvalues.Add(INIval)
File.WriteList(File.DirRootexternal, "TEST.ini", INIvalues)
End Sub

Sub LOAD_Click
INIvalues = File.ReadList(File.DirRootexternal, "TEST.ini")
INIval = INIvalues.Get(0)
RESULT.Text = INIval.FamilyName & " " & INIval.Age
End Sub
 

fixit30

Active Member
Licensed User
Longtime User
Because you are using a list of custom types this will not work.

You will need to use the RandomAccessFile library and use the WriteObject and ReadObject methods.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I also don't understand the reason by I managed to get the data like this:
B4X:
Sub LOAD_Click
INIvalues = File.ReadList(File.DirRootexternal, "TEST.ini")
INIval = INIvalues.Get(0)
Dim str(), stn(),sta() As String
str = Regex.Split(",",INIval)
stn = Regex.Split("=",str(0))
sta = Regex.Split("=",str(1))
RESULT.Text = stn(1) & "  " & sta(1)
End Sub

I guess you should use a RandomAccessFile and not a text file for a list of custom type. (Written before I saw the previous post...)
 
Upvote 0
Top