Android Question How to Instantly Update PCLV/CLV Item to reflect changes from List of Files in a Folder(solved)

omo

Active Member
Licensed User
Longtime User
How can one instantly Update PCLV/CLV Item views to reflect changes from List of Files in a target Folder . Looking at the image below adapted from preoptimized CLV given as example from Erel. I want instant item to show in customlistview for example as item 7 in the image below when new images is submitted to a folder with list of images in dirinternal without recreating activity as in using: B4XPage_Created(Root). Once i click submit or refresh button, last item submitted to List of Files in a target Folder should reflect as no 7 item in the image below instantly without recreating activity as usually experienced when B4XPage_Created(Root) is used.
cd.png


This is how affected part of my code look like. I just need something like update codei.e sub update that when clicked or put somewhere within the code will update CLV alone to display new item in the list without recreating activity:
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Try
   
        Root = Root1
        'load the layout to Root
        Root.LoadLayout("livescangB2")
   
        recognizer.Initialize
    
        chooser.Initialize(Me, "chooser")
       
        TextEngine.Initialize(Root)
       
        BBScrollingLabel1.TextEngine = TextEngine
       
        'clv stats......................
        If File.IsDirectory(xui.DefaultFolder, B4XPages.MainPage.ocrbasefolder & "/ultimateocr") = False Then
            File.MakeDir(xui.DefaultFolder, B4XPages.MainPage.ocrbasefolder & "/ultimateocr")
        End If
   
        If File.IsDirectory(xui.DefaultFolder, B4XPages.MainPage.ocrbasefolder & "/ultimateocr" &"/workplace") = False Then
            File.MakeDir(xui.DefaultFolder, B4XPages.MainPage.ocrbasefolder & "/ultimateocr" &"/workplace")
        End If

        CustomListView1.AnimationDuration = 0
   
        fp.Initialize(Me,"fp",Root)
   
        Dialog2.Initialize(Root)
        Dialog2.Title = "Enter/Edit Album Info"
  
        PCLV.Initialize(Me, "PCLV", CustomListView1)
       
        List1.Initialize
       
        PCLV.ShowScrollBar = False 'True 'False 'no fast scrolling
        PCLV.ExtraItems = 3
       
        Dim heightbb As Int
        If xui.IsB4J Then heightbb = 200dip Else heightbb = 150dip
   
        'list files from folderand add each of tem to list1
        Wait For (File.ListFilesAsync(xui.DefaultFolder&"/" & B4XPages.MainPage.ocrbasefolder &"/ultimateocr")) Complete (Success As Boolean, Files As List)
        If Success Then
            For Each f As String In Files
'            If    filenm2b.EndsWith(".jpg") Or filenm2b.EndsWith(".png")  Or filenm2b.EndsWith(".bmp") And Not(filenm2b.EndsWith(".ini")) Then
                If f.EndsWith(".jpg") Or f.EndsWith(".png")  Or f.EndsWith(".bmp")  Or f.EndsWith(".jpeg")  Or f.EndsWith(".webp") Or f.EndsWith(".gif") Or f.EndsWith(".pdf") And Not(f.EndsWith(".ini")) Then ' And Not(f.EndsWith(".ini")
                    List1.Add(File.Combine(xui.DefaultFolder&"/" &B4XPages.MainPage.ocrbasefolder &"/ultimateocr", f))
                End If
               
            Next
         
        End If
       
       
        For i = 0 To List1.Size-1 Step 3 'add from list 'i changed from 0
            Log(i)
            PCLV.AddItem(heightbb, xui.Color_White, CreateMyImageData(i))
        Next
        '...........................
 

        Next
   
        '........................
        PCLV.ShowScrollBar = False 'no fast scrolling
        '    PCLV.ExtraItems = 3
        PCLV.Commit
      
   
    Catch
        Log(LastException)
    End Try
End Sub


Sub PCLV_HintRequested (Index As Int) As Object
'    Dim word As String = CustomListView1.GetValue(Index)
    Return CreateMyImageData(Index)
End Sub

Sub CustomListView1_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)
    Try

        For Each i As Int In PCLV.VisibleRangeChanged(FirstIndex, LastIndex)
            Dim item As CLVItem = CustomListView1.GetRawListItem(i)
            pnl = xui.CreatePanel("")
            item.Panel.AddView(pnl, 0, 0, item.Panel.Width, item.Panel.Height) 'item.Panel.Width, item.Panel.Height
            Dim data As MyImageDatafavmix = item.Value
           
           
            'Create the item layout
            pnl.LoadLayout("itemocr")
           
            Wait For (File.ListFilesAsync(xui.DefaultFolder&"/" & B4XPages.MainPage.ocrbasefolder &"/ultimateocr")) Complete (Success As Boolean, Files As List)
            If Success Then
           
           
                For x = 0 To 2
                   
                   
                    Dim t As Int = data.IndexOfFirstImage + x
                    If t >= List1.Size Then '
                        Exit
                    End If

           
                    filenm2b = (List1.Get(t)) 'i should always take note in b4xtable or album.can be used for tag to identify each cell
                    filenm2b = filenm2b.SubString(filenm2b.LastIndexOf("/") + 1)
                    Dim idx As Int = data.IndexOfFirstImage + x
                    pnl.GetView(x+3).GetView(0).Text =  idx & "Img2Txt:" & $"$1.2{File.Size(xui.DefaultFolder, B4XPages.MainPage.ocrbasefolder &"/ultimateocr" & "/" & filenm2b) / 1024 / 1024} MB"$ 'filenm2b
                   

               
                    If    filenm2b.EndsWith(".jpg") Or filenm2b.EndsWith(".png")  Or filenm2b.EndsWith(".bmp") Or filenm2b.EndsWith(".jpeg")  Or filenm2b.EndsWith(".webp") And Not(filenm2b.EndsWith(".ini")) Then


                        pnl.GetView(x).Tag = filenm2b
                        Try
'                       
                            pnl.GetView(x).GetView(0).LoadLayout("b4ximagealb")
                            B4XImageView1.Load(xui.DefaultFolder, B4XPages.MainPage.ocrbasefolder &"/ultimateocr" & "/"&filenm2b)
                            B4XImageView1.ResizeMode = "FILL_NO_DISTORTIONS"
                           
                            B4XImageView1.mBase.Left = 0%x
                            B4XImageView1.mBase.Width = 100%x  ' CustomListView1.GetPanel(i).width '100%x
                            B4XImageView1.mBase.Top = 0%y
                            B4XImageView1.mBase.Height = 100%y
                           
                    
                        Catch
                            Log("Error loading faq.html: " & LastException.Message)
                        End Try
                   


                    Sleep(0)
                   
                    swtbutton1(filenm2b,pnl.GetView(x)) 'left
                    swtbutton2(filenm2b,pnl.GetView(x)) 'right
                    swtbutton3(filenm2b,pnl.GetView(x)) 'buttom
        
                   
                Next
               
               
            End If

        Next
 
        AdvancedSnap.Initialize(CustomListView1)
        AdvancedSnap.InstantSnap = True
   
    Catch
        Log(LastException)
    End Try
   

End Sub
 
Last edited:
Solution
Closed! !!
The problem lies in some oversight mistakes and unnecessary library added to my code. Original concept works as I wanted. I still ended up using B4XPage_Created(Root) to refresh after submitting, but introduced delay of minimum sleep(1000) to allow page recreation finish before continue. The logic works well

omo

Active Member
Licensed User
Longtime User
It seems this may solve my problem, but I am yet to try it. I will do that once I close to my computer to update this thread:

CustomListView1.InsertAtTextItem(Index, "New Item", "")
PCLV.ListChangedExternally

However, the thread still opens for the answer until I update this thread after testing my new finding.
 
Upvote 0

omo

Active Member
Licensed User
Longtime User
Consider using CLV with lazy loading. It will be simpler and more flexible than PCLV. PCLV is useful when you are working with huge lists (10,000+).
Thank you Erel, I was even planning to close the thread today or tomorrow before. While trying this idea of using:
CustomListView1.InsertAtTextItem(Index, "New Item", "")
PCLV.ListChangedExternally which did not solve my problem, but opened my eyes to recheck my original code idea where I discovered some errors and unnecessary advancedsnap library that I added to the code which I removed and corrected. I have resolved the issue, but only got hooked up by another very minor thing which I hope to resolve today as well.

I purposely used PCLV because you already divided it into columns which I just reduced to three and you made simpler for me to adjust for my need to host more files like video, audio, pdf, animation etc, and also because I don't know how each user may decide to grow the file folder for images or other media into several thousands.

In other parts of my code, I already used CLV with lazy loading (that of card list) for other things which was indeed easier but single column, but I don't want to start dividing the column from the scratch. In a nutshell, the issue has been resolved; I will come and close down this thread once I resolve other minor thing I am facing. Thank you very much, Erel; I am grateful
 
Upvote 0

omo

Active Member
Licensed User
Longtime User
Closed! !!
The problem lies in some oversight mistakes and unnecessary library added to my code. Original concept works as I wanted. I still ended up using B4XPage_Created(Root) to refresh after submitting, but introduced delay of minimum sleep(1000) to allow page recreation finish before continue. The logic works well
 
Upvote 0
Solution
Top