Android Question KeyValueStore: reading a custom type array

peacemaker

Expert
Licensed User
Longtime User
HI, All

If i have such custom type:
B4X:
    Type FTPEntry2(Name As String, Timestamp As Long, Size As Long, FTP_path As String, LocalPath As String, status As String)    'writable file info structure (FTP FTPEntry structure is read-only)
    Public FTPfiles() As FTPEntry2    'current FTP files info to compare
    Private PrevFiles() As FTPEntry2
    Private kvs As KeyValueStore   'v.2.30

Saving the array into is OK, but how to read and assign correctly ?

B4X:
        Dim obj As Object = kvs.Get("ftp_entries")     'it's working OK, but ...
        PrevFiles = obj    'but here the error:
      
        java.lang.IllegalArgumentException: field b4a.example.aftclient.aftpclient._prevfiles has type b4a.example.aftclient.aftpclient$_ftpentry2[], got java.lang.Object[]
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Don, sure - this straight way was used initially, and it's with the same error.
Getting as Object is OK, but ... how to assign...

If impossible by KVS - question is - how to save\load such custom typed arrays ?
 
Last edited:
Upvote 0

Geezer

Active Member
Licensed User
Longtime User
B4X:
Public FTPfiles() As FTPEntry2    'current FTP files info to compare
Would this not make sense to be a list ?

You can read the list to store into kvs and also read from kvs to build the list.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
B4X:
java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.util.List

Erel's post mentioned some "CreateCustomType" sub, no idea what this is...
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
try
B4X:
Public FTPfiles As List = kvs.Get("ftp_entries")
Already tried, it's assigned as list, but if try to use the list -
java.lang.Object[] cannot be cast to java.util.List
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Already tried, it's assigned as list
if the assignment is not raising any error then the problem is in the try ti use you are talking about.
You are not providing any useful code-snippets what exactly you are writing to the kvs and how.
Even not the code used to use the written data.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
the same FTPEntry2.
Seems, only objects are supported in KVS, as Erel's post states.
But how to use custom types... only by RandomAccessFile.WriteObject and ReadObject ?

I just try to use the latest modern classes, seems, KVS is the latest one for save\load variables to a file.



B4X:
    If kvs.ContainsKey("ftp_entries") Then
        Dim obj As Object = kvs.Get("ftp_entries")
        Dim L As List = obj
        For i = 0 To L.Size - 1                'here error: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.util.List
            PrevFiles(i) = L.Get(i)
        Next
    End If
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
building the List
B4X:
'save the FTP files list info
Sub Copy_TO_FTPEntry2(source() As FTPEntry)
    Dim FTPfiles(source.Length) As FTPEntry2
    For i = 0 To source.Length - 1
        FTPfiles(i).Name = source(i).Name
        FTPfiles(i).Size = source(i).Size
        FTPfiles(i).Timestamp = source(i).Timestamp
        FTPfiles(i).IsInitialized = source(i).IsInitialized
    Next
End Sub

FTPEntry of FTP lib is read-only, so i have to make custom FTPEntry2 to manipulate by the files.
Comparing by use previouse PrevFiles() and current array FTPfiles().

And task is just to save to a file and read back...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Copy_TO_FTPEntry2(source() As FTPEntry) As List

    Dim FTPfiles As List
    FTPfiles.initialize
    For i = 0 To source.Length - 1
        Dim src As FTPEntry = source(i)
        Dim fle As FTPEntry2 = CreateFTPEntry2(src.Name,src.Timestamp,src.Size,"","","")
        FTPfiles.Add(fle)
    Next
    Return FTPfiles
End Sub
Public Sub CreateFTPEntry2 (Name As String, Timestamp As Long, Size As Long, FTP_path As String, LocalPath As String, status As String) As FTPEntry2
    Dim t1 As FTPEntry2
    t1.Initialize
    t1.Name = Name
    t1.Timestamp = Timestamp
    t1.Size = Size
    t1.FTP_path = FTP_path
    t1.LocalPath = LocalPath
    t1.status = status
    Return t1
End Sub

write the list to the kvs
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
write the list to the kvs

Ha, Don ! So, you offered the standard solution based on List , instead of arrays, and it works. Seems, custom type arrays impossible to save\load easily by anoter way.
Thanks, donated for the help.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…