Android Question [SOLVED] GIFs cannot be displayed in all use PreoptimizedCLV

winjiadh

Active Member
Licensed User
Longtime User
I downloaded the PreoptimizedCLV library
Modified some of the code to display long images normally, screenshots are as follows
20230606111559.jpg

I wanted to replace these images with GIFs or video sources, so I modified the original code further
I added a list variable Private csvList As List and opened a csv file
Read the URL of the GIF image from the CSV file
B4X:
Private csvList As List
csvList.Initialize
    Dim InputStream1 As InputStream
    InputStream1 = File.OpenInput(File.DirInternal, "mycsvfile.csv")
    Dim TextReader1 As TextReader
    TextReader1.Initialize(InputStream1)
    Dim Line As String
    Do While TextReader1.Ready = True
        Line = TextReader1.ReadLine
        Dim Fields() As String
        Fields = Regex.Split(",", Line)
        Dim level As String = Fields(0)
        Dim url As String = Fields(1)
        Dim name As String = Fields(2)
        Dim image As String = Fields(3)
        Dim item As Map
        item.Initialize
        item.Put("level", level)
        item.Put("url", url)
        item.Put("name", name)
        item.Put("image", image)
        csvList.Add(item)
    Loop
I modified part of the code of CustomListView1_VisibleRangeChanged
B4X:
Sub CustomListView1_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    For Each i As Int In PCLV.VisibleRangeChanged(FirstIndex, LastIndex)
        If i>=LastIndex+1 Then Exit
        Dim item As CLVItem = CustomListView1.GetRawListItem(i)
        Dim pnl As B4XView = xui.CreatePanel("")
        item.Panel.AddView(pnl, 0, 0, item.Panel.Width, item.Panel.Height)
        Dim data As MyImageData = item.Value
        'Create the item layout
        pnl.LoadLayout("item")
        Dim itmap As Map
        itmap = csvList.Get(i+1)
        Dim imgurl As String
        imgurl=itmap.Get("image")
        pnl.GetView(0).GetView(1).Text = itmap.Get("url")'data.IndexOfFirstImage '+ x
        MediaManager.SetMedia(pnl.GetView(0).GetView(0),imgurl)'    MediaManager.SetMedia(pnl.GetView(0).GetView(0), $"https://picsum.photos/id/${data.IndexOfFirstImage}/500/100.jpg"$)

    Next
    'The purpose of this call is to find panels that are no longer in the views tree and cancel any ongoing request that is no longer relevant.
    'It also happens automatically from time to time but in this case, as many requests are sent frequently, it is better to force it to trim the cache.
    'Note that you need to add HU2_PUBLIC to the build configuration.
    MediaManager.TrimMediaCache
End Sub

The image can only display the first 5 URLs of the CSV file, and the following ones cannot be displayed, and when you swipe up and down, it will also report an error exit
20230606111958.jpg

Hope someone can help me down, thank you very much
 

Attachments

  • B4AProject.zip
    12.4 KB · Views: 47
Last edited:
Solution
would you told me, How to replace the TexReader code with StringUtils?
Use this code to use stringUtils instead of textreader.
B4X:
Dim su As StringUtils  'mahares
    Dim MyList As List
    MyList.Initialize
    Dim header As List
    header.Initialize
    MyList = su.LoadCSV2(xui.DefaultFolder, "mycsvfile.csv",",", header)
    For i=0 To MyList.Size-1
        Dim row() As String =MyList.Get(i)
        Dim level As String = row(0)
        Dim url As String =  row(1)
        Dim name As String =  row(2)
        Dim image As String =  row(3)
        Dim item As Map
        item.Initialize
        item.Put("level", level)
        item.Put("url", url)
        item.Put("name", name)
        item.Put("image", image)...

winjiadh

Active Member
Licensed User
Longtime User
sorry, I used the Export zip. mycsvfile.csv can't included.
I add it to the files directory in the B4AProject.zip
 

Attachments

  • B4AProject.zip
    12.8 KB · Views: 43
Upvote 0

winjiadh

Active Member
Licensed User
Longtime User
The CSV file is missing.

Tip: never use TextReader unless you need to read non-utf8 files.
Use File.ReadList or StringUtils.LoadCSV instead.
sorry, I used the Export zip. mycsvfile.csv can't included.
I add it to the files directory in the B4AProject.zip
can you help me?
please
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
can you help me?
please
I made a couple of changes to your below sub and now it works. Good luck. If you also want to replace the TexReader code with StringUtils come back
B4X:
Sub CustomListView1_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    For Each i As Int In PCLV.VisibleRangeChanged(FirstIndex, LastIndex)
'        If i>=LastIndex+1 Then Exit   'mahares commented
        Dim item As CLVItem = CustomListView1.GetRawListItem(i)
        Dim pnl As B4XView = xui.CreatePanel("")
        item.Panel.AddView(pnl, 0, 0, item.Panel.Width, item.Panel.Height)
'        Dim data As MyImageData = item.Value
        pnl.LoadLayout("item")
        Dim itmap As Map
        itmap = csvList.Get(i)  'mahares
'        itmap = csvList.Get(i+1)
        Dim imgurl As String
        imgurl=itmap.Get("image")
        pnl.GetView(0).GetView(1).Text = itmap.Get("url")'data.IndexOfFirstImage '+ x
        MediaManager.SetMedia(pnl.GetView(0).GetView(0),imgurl)'    MediaManager.SetMedia(pnl.GetView(0).GetView(0), $"https://picsum.photos/id/${data.IndexOfFirstImage}/500/100.jpg"$)
    Next
    MediaManager.TrimMediaCache
End Sub
1686052166653.png
 
Last edited:
Upvote 0

winjiadh

Active Member
Licensed User
Longtime User
I made a couple of changes to your below sub and now it works. Good luck. If you also want to replace the TexReader code with StringUtils come back
B4X:
Sub CustomListView1_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    For Each i As Int In PCLV.VisibleRangeChanged(FirstIndex, LastIndex)
'        If i>=LastIndex+1 Then Exit   'mahares commented
        Dim item As CLVItem = CustomListView1.GetRawListItem(i)
        Dim pnl As B4XView = xui.CreatePanel("")
        item.Panel.AddView(pnl, 0, 0, item.Panel.Width, item.Panel.Height)
'        Dim data As MyImageData = item.Value
        pnl.LoadLayout("item")
        Dim itmap As Map
        itmap = csvList.Get(i)  'mahares
'        itmap = csvList.Get(i+1)
        Dim imgurl As String
        imgurl=itmap.Get("image")
        pnl.GetView(0).GetView(1).Text = itmap.Get("url")'data.IndexOfFirstImage '+ x
        MediaManager.SetMedia(pnl.GetView(0).GetView(0),imgurl)'    MediaManager.SetMedia(pnl.GetView(0).GetView(0), $"https://picsum.photos/id/${data.IndexOfFirstImage}/500/100.jpg"$)
    Next
    MediaManager.TrimMediaCache
End Sub
View attachment 142651
thanks,you are great!
would you told me, How to replace the TexReader code with StringUtils?
thanks
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
would you told me, How to replace the TexReader code with StringUtils?
Use this code to use stringUtils instead of textreader.
B4X:
Dim su As StringUtils  'mahares
    Dim MyList As List
    MyList.Initialize
    Dim header As List
    header.Initialize
    MyList = su.LoadCSV2(xui.DefaultFolder, "mycsvfile.csv",",", header)
    For i=0 To MyList.Size-1
        Dim row() As String =MyList.Get(i)
        Dim level As String = row(0)
        Dim url As String =  row(1)
        Dim name As String =  row(2)
        Dim image As String =  row(3)
        Dim item As Map
        item.Initialize
        item.Put("level", level)
        item.Put("url", url)
        item.Put("name", name)
        item.Put("image", image)
        csvList.Add(item)
    Next

replace or comment the textreader code from this line: Dim InputStream1 As InputStream to this line:TextReader1.Close
 
Upvote 1
Solution

winjiadh

Active Member
Licensed User
Longtime User
Use this code to use stringUtils instead of textreader.
B4X:
Dim su As StringUtils  'mahares
    Dim MyList As List
    MyList.Initialize
    Dim header As List
    header.Initialize
    MyList = su.LoadCSV2(xui.DefaultFolder, "mycsvfile.csv",",", header)
    For i=0 To MyList.Size-1
        Dim row() As String =MyList.Get(i)
        Dim level As String = row(0)
        Dim url As String =  row(1)
        Dim name As String =  row(2)
        Dim image As String =  row(3)
        Dim item As Map
        item.Initialize
        item.Put("level", level)
        item.Put("url", url)
        item.Put("name", name)
        item.Put("image", image)
        csvList.Add(item)
    Next

replace or comment the textreader code from this line: Dim InputStream1 As InputStream to this line:TextReader1.Close
thanks, you are very great!
 
Upvote 0
Top