Italian Leggere una List di Type

Sabotto

Active Member
Licensed User
Semplicissimo test: Creo una List con dei type, la salvo in un file e quando la leggo ricevo l'errore. Perchè?
Test in B4J (ma credo sia uguale in B4A)

Error occurred on line: 54 (B4XMainPage)
java.lang.ClassCastException: class java.lang.String cannot be cast to class b4j.example.b4xmainpage$_persona (java.lang.String is in module java.base of loader 'bootstrap'; b4j.example.b4xmainpage$_persona is in unnamed module of loader 'app')
at b4j.example.b4xmainpage._button3_click(b4xmainpage.java:135)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
...
ecc


B4X:
Sub Class_Globals
    
    Private Root As B4XView
    Private xui As XUI
    Private Const FileNameList As String = "LaLista.txt"
    Type Persona(Nome As String, Cognome As String, Eta As Int, Hobby(3) As String)
        
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True

    xui.SetDataFolder("ZZZmiaCartella")
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

Private Sub Button3_Click
    
    'Creazione di una Lit di type e salvataggio del file
    Dim aList1 As List
    aList1.Initialize

    Dim mHobby(2) As String

    mHobby(0)="Sport"
    mHobby(1)="Musica"
    aList1.Add(CreaPersona("ABATE","ALDO", 12,mHobby))
    
    mHobby(0)="Canto"
    mHobby(1)="Caccia"
    aList1.Add(CreaPersona("BALDI","BRUNO", 32, mHobby))
        
    File.WriteList(xui.DefaultFolder,FileNameList,aList1)

    'Lettura del file e memorizzazione nell List
    aList1.Clear
    aList1=File.ReadList(xui.DefaultFolder, FileNameList)
        
    Dim i As Int
    For i = 0 To aList1.Size-1
        Dim mPer As Persona = aList1.Get(i) '====>>QUI DA' ERRORE'

' Anche usando 
For each mPer as Persona in aList1 
'ottengo errore. Perchè no posso usarlo?
 
       Log(mPer.Nome)
    Next
    
End Sub


Public Sub CreaPersona (Nome As String, Cognome As String, Eta As Int, Hobby() As String) As Persona
    Dim aPerson As Persona
    aPerson.Initialize
    aPerson.Nome = Nome
    aPerson.Cognome = Cognome
    aPerson.Eta = Eta
    aPerson.Hobby = Hobby
    Return aPerson
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
Perché non puoi scrivere/leggere una List che contenga custom type. Devi usare la serializzazione per salvare la lista.

Nota che sarebbe anche errato, quel codice, perché mHobby andrebbe reinizializzato, prima di aggiungerlo alla List.
 

Xfood

Expert
Licensed User
ti aggiungo queste due funzioni che ho trovato qui nel sito

prova a salvare la lista e poi recuperarla con queste 2 funzioni


B4X:
Public Sub WriteTypedList(Directory As String, Filename As String, TheList As List)    ' requires RandomAccessFile library
    Dim poRaf As RandomAccessFile
    poRaf.Initialize(Directory, Filename, False)
    poRaf.WriteB4XObject(TheList, 0)
    poRaf.Close
End Sub

Public Sub ReadTypedList(Directory As String, Filename As String) As List
    Dim poList As List

    poList.Initialize
    If File.Exists(Directory, Filename) Then
        Try
            Dim poRaf As RandomAccessFile
            poRaf.Initialize(Directory, Filename, True)
            poList = poRaf.ReadB4XObject(0)
            poRaf.Close
        Catch
            Log(LastException)
        End Try
    End If

    Return poList
End Sub
 

Sabotto

Active Member
Licensed User
Nota che sarebbe anche errato, quel codice, perché mHobby andrebbe reinizializzato, prima di aggiungerlo alla List.
Spiegami meglio. In che posto del codice dovrei farlo e come? con un nuovo
B4X:
Dim mHobby(2) as string
?
 

Sabotto

Active Member
Licensed User
Ho uato le routine di Xfood ma ancora ricevo errore nella Sub ReadTypedList
(vedi il Rem)

B4X:
Private Sub Button3_Click
   
    'Creazione di una Lit di type e salvataggio del file
    Dim aList1 As List
    aList1.Initialize

    Dim mHobby(2) As String
    mHobby(0)="Sport"
    mHobby(1)="Musica"
    aList1.Add(CreaPersona("ABATE","ALDO", 12,mHobby))
   
    Dim mHobby(2) As String
    mHobby(0)="Canto"
    mHobby(1)="Caccia"
    aList1.Add(CreaPersona("BALDI","BRUNO", 32,mHobby))
       
    'File.WriteList(xui.DefaultFolder,FileNameList,aList1)
    WriteTypedList(xui.DefaultFolder,FileNameList,aList1)
   
    'Lettura del file e memorizzazione nell List
    aList1.Clear
    'aList1=File.ReadList(xui.DefaultFolder, FileNameList)
    aList1=ReadTypedList(xui.DefaultFolder, FileNameList)
    Dim i As Int
    For i = 0 To aList1.Size-1
        Dim mPer As Persona = aList1.Get(i)
        Log(mPer.Nome)
    Next
   
End Sub


Public Sub CreaPersona (Nome As String, Cognome As String, Eta As Int, Hobby() As String) As Persona
    Dim aPerson As Persona
    aPerson.Initialize
    aPerson.Nome = Nome
    aPerson.Cognome = Cognome
    aPerson.Eta = Eta
    aPerson.Hobby = Hobby
    Return aPerson
End Sub

Sub WriteTypedList(Directory As String, Filename As String, TheList As List)    ' requires RandomAccessFile library
    Dim poRaf As RandomAccessFile
    poRaf.Initialize(Directory, Filename, False)
    poRaf.WriteB4XObject(TheList, 0)
    poRaf.Close
End Sub

Sub ReadTypedList(Directory As String, Filename As String) As List
    Dim poList As List

    poList.Initialize
    If File.Exists(Directory, Filename) Then
        Try
            Dim poRaf As RandomAccessFile
            poRaf.Initialize(Directory, Filename, True)
            poList = poRaf.ReadB4XObject(0)
            poRaf.Close
        Catch
            Log(LastException)
            'ERRORE
            '(IllegalArgumentException) java.lang.IllegalArgumentException:
            'Can not set [Ljava.lang.String; field b4j.example.b4xmainpage$_persona.Hobby to [Ljava.lang.Object;
        End Try
    End If
    Return poList
End Sub
 

Xfood

Expert
Licensed User
Guarda l'esempio di erel
 

LucaMs

Expert
Licensed User
Longtime User
La serializzazione pare non accettare l'array come facente parte del custom type. Sostituiscilo con una List.

B4X:
'La serializzazione richiede che la dichiarazione del tipo custom stia nel Main o nella B4XMainPage
Type Persona(Nome As String, Cognome As String, Eta As Int, Hobby As List)


Private Const FileNameList As String = "LaLista.txt"

Private Sub Button3_Click

    Dim lstHobby As List
    lstHobby.Initialize
    lstHobby.Add("Sport")
    lstHobby.Add("Musica")
    aList1.Add(CreatePersona("ABATE","ALDO", 12, lstHobby))
 
    Dim lstHobby As List
    lstHobby.Initialize
    lstHobby.Add("Canto")
    lstHobby.Add("Caccia")
    aList1.Add(CreatePersona("BALDI","BRUNO", 32, lstHobby))

    WriteTypedList(xui.DefaultFolder,FileNameList,aList1)
 
    'Lettura del file e memorizzazione nell List
    aList1.Clear
    aList1=ReadTypedList(xui.DefaultFolder, FileNameList)
 
    For Each Tizio As Persona In aList1
        Log(Tizio.Nome & TAB & Tizio.Cognome & TAB & Tizio.Eta)
        For Each strHobby As String In Tizio.Hobby
            Log(TAB & strHobby)
        Next
    Next

End Sub


Public Sub WriteTypedList(Directory As String, Filename As String, TheList As List)    ' requires RandomAccessFile library
    Dim poRaf As RandomAccessFile
    poRaf.Initialize(Directory, Filename, False)
    poRaf.WriteB4XObject(TheList, 0)
    poRaf.Close
End Sub

Public Sub ReadTypedList(Directory As String, Filename As String) As List
    Dim poList As List

    poList.Initialize
    If File.Exists(Directory, Filename) Then
        Try
            Dim poRaf As RandomAccessFile
            poRaf.Initialize(Directory, Filename, True)
            poList = poRaf.ReadB4XObject(0)
            poRaf.Close
        Catch
            Log(LastException)
        End Try
    End If

    Return poList
End Sub


Public Sub CreatePersona (Nome As String, Cognome As String, Eta As Int, Hobby As List) As Persona
    Dim t1 As Persona
    t1.Initialize
    t1.Nome = Nome
    t1.Cognome = Cognome
    t1.Eta = Eta
    t1.Hobby = Hobby
    Return t1
End Sub
 
Last edited:

Sabotto

Active Member
Licensed User
Anche se era del tutto inutile ho provato lo stesso ed effettivamente.... era inutile provare.. 👍
 
Top