Error reading from a list

splatt

Active Member
Licensed User
Longtime User
My application creates a playlist, using the following type declaration:

B4X:
Type PlayListItem(Artist As String, Album As String, Track As String)

This is then used to create playlist items:
B4X:
   Dim pli As PlayListItem
   pli.Initialize
   pli.Track = CleanString(Value)
   pli.Artist = CleanString(CurrPath.Get(0))
   pli.Album = CleanString(CurrPath.Get(1))
   PlayList.Add(pli)

The PlayList is save to a file with:
B4X:
File.WriteList( File.DirDefaultExternal, CurrentPL, PlayList)

Subsequently the list is then loaded again with:
B4X:
If fdlg.Show("Select Playlist","OK","Cancel","",  LoadBitmap(File.DirAssets, "play-button-th.png")) = DialogResponse.POSITIVE Then
   target = fdlg.ChosenName
   PlayList.Clear
   PlayList = File.ReadList( File.DirDefaultExternal, target)
   imgPlayList_Click
End If

When I try to access the individual list items, with:
B4X:
   For t = 0 To PlayList.Size - 1
      Dim pli As PlayListItem
      'pli.Initialize
      pli = PlayList.Get(t) 
      lvPlayList.AddTwoLines2(pli.Artist & " : " & pli.Track, pli.Album, pli.Track)      
   Next

I get this error:
java.lang.ClassCastException: java.lang.String cannot be cast to remoteplayer.b4a.client$_playlistitem

Anyone suggest a way to get the contents of the PlayList please?
 

splatt

Active Member
Licensed User
Longtime User
File.WriteList writes a list of strings. You should use RandomAccessFile.WriteObject to write the complete list.

Thanks for the reply Erel.

Can I then use File.ReadList to read it in again?
 
Upvote 0

splatt

Active Member
Licensed User
Longtime User
Can the list be written and read back in one go, or does one need to iterate through the list and write/read each list item individually?

Are there examples of this?
 
Upvote 0
Top