Android Question Lazy Loading for Nested CustomListView

joko0124

Member
Licensed User
Longtime User
Hi! Is there anyone here I can ask for advice on how to improve the loading speed of data from a local database into my CustomListView?
If possible, I would like to implement lazy loading to make data rendering more efficient.

Below is my existing code, which I also found here on the forum a long time ago.
Thank you in advance to anyone who can help.

B4X:
Private Sub GetRecords(iAreadID As Int)
    Dim iBranchID As Int
    Dim sBranchName As String
    Dim rsBranches As Cursor
    
    ProgressDialogShow2($"Reading Employee Info..."$, False)
    clvEmployees.Clear
    Try
        Starter.strCriteria = "SELECT BranchID, BranchName FROM tblBranches " & _
                              "WHERE AreaID = " & iAreadID & " " & _
                              "ORDER BY BranchID"
        LogColor(Starter.strCriteria, Colors.Yellow)
        
        rsBranches = Starter.DBCon.ExecQuery(Starter.strCriteria)
        
        If rsBranches.RowCount > 0 Then
            LogColor($"Record Found: ${rsBranches.RowCount}"$, Colors.Yellow)
            For x = 0 To rsBranches.RowCount - 1
                rsBranches.Position = x
                iBranchID = rsBranches.GetInt("BranchID")
                sBranchName = rsBranches.GetString("BranchName")
                
                AddBranches(sBranchName)
                ' Add the branch to the list
            
                ' Fetch and load employees for this branch
                LoadEmployeesForBranch(iBranchID)
            Next
        Else
            clvEmployees.AddTextItem($"No Record Found..."$,"")
        End If
        lblRecCount.Text = $"${DBFunctions.GetTotalAttendees(iAreadID)} Record(s) found..."$
        lblUnregCount.Text = $" UNREGISTERED: ${DBFunctions.GetTotUnRegister(iAreadID)}"$
    Catch
        Log(LastException)
    End Try
End Sub

Private Sub AddBranches(sBranch As String)
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, clvEmployees.AsView.Width, TitleHeight)
    p.LoadLayout("BranchList")
    
    Dim BranchRec As BranchInfo
    BranchRec.sBranchName = sBranch
    
    lblBranchName.Text = sBranch
    LogColor(sBranch, Colors.Red)
    
    clvEmployees.Add(p, BranchRec)
End Sub

Sub LoadEmployeesForBranch(BranchID As Int)
    Dim rsEmployees As Cursor
    Try
        Starter.strCriteria = "SELECT Employees.RegID, Employees.BranchID, " & _
                              "substr(FirstName, 1, 1) || substr(LastName,1,1) AS Avatar, Employees.FullName, Employees.DivisionName, " & _
                              "Employees.TableNo, Employees.WillAttend AS RegStatus, Employees.WasRegistered " & _
                              "FROM tblRegistration AS Employees " & _
                              "WHERE Employees.BranchID = " & BranchID & " " & _
                              "AND Employees.WillAttend > 0 " & _
                              "ORDER BY Employees.LastName ASC"
        LogColor(Starter.strCriteria, Colors.Cyan)

        rsEmployees = Starter.DBCon.ExecQuery(Starter.strCriteria)

        If rsEmployees.RowCount > 0 Then
            LogColor($"Record Found: ${rsEmployees.RowCount}"$, Colors.Yellow)
            TotalRec = TotalRec + rsEmployees.RowCount
            For i = 0 To rsEmployees.RowCount - 1
                rsEmployees.Position = i
                Dim EmpRec As EmpInfo
                EmpRec.iRegID = rsEmployees.GetInt("RegID")
                EmpRec.sAvatar = rsEmployees.GetString("Avatar")
                EmpRec.sFullName = rsEmployees.GetString("FullName")
                EmpRec.sDivisionName = rsEmployees.GetString("DivisionName")
                EmpRec.iWillAttend = rsEmployees.GetInt("RegStatus")
                EmpRec.iWasRegistered = rsEmployees.GetInt("WasRegistered")
                EmpRec.sTableNo = rsEmployees.GetString("TableNo")
                EmpRec.iBranchID = rsEmployees.GetInt("BranchID")
                ' Add employee data to the list
                
                clvEmployees.Add(GenerateList(clvEmployees.AsView.Width, EmpRec.sAvatar, EmpRec.sFullName, EmpRec.sDivisionName, EmpRec.iWillAttend, EmpRec.iWasRegistered), EmpRec)
            Next
            ProgressDialogHide
        Else
            ProgressDialogHide
        End If
    Catch
        Log(LastException)
    End Try
End Sub

Sub GenerateList(iWidth As Int, Avatar As String, EmpName As String, DivisionName As String, WillAttend As Int, RegStatus As Int) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    Dim iHeight As Int = 135dip

    If GetDeviceLayoutValues.ApproximateScreenSize < 4.5 Then iHeight = 310dip
    p.SetLayoutAnimated(0, 0, 50dip, iWidth, iHeight)
    p.LoadLayout("EmployeeList")
    
    If WillAttend = 1 Then
        If RegStatus = 0 Then
            csStatus.Initialize.Bold.Color(Colors.Red).Append("UNREGISTERED").PopAll
            EnableRegButton(btnRegister)
            btnRegister.Text = "REGISTER"
            DisableButton(btnSwap)
            btnSwap.Text = "REPRINT STUB"
        Else
            csStatus.Initialize.Bold.Color(0xFF1976D2).Append("REGISTERED").PopAll
            DisableButton(btnRegister)
            btnRegister.Text = "REGISTER"
            EnableReprintButton(btnSwap)
            btnSwap.Text = "REPRINT STUB"
        End If
    Else
        If RegStatus = 0 Then
            csStatus.Initialize.Bold.Color(Colors.Red).Append("UNREGISTERED").PopAll
            DisableButton(btnRegister)
            btnRegister.Text = "REGISTER"
            EnableSwapButton(btnSwap)
            btnSwap.Text = "SWAP DUTY"
        Else
            csStatus.Initialize.Bold.Color(0xFF1976D2).Append("REGISTERED").PopAll
            DisableButton(btnRegister)
            btnRegister.Text = "REGISTER"
            EnableReprintButton(btnSwap)
            btnSwap.Text = "REPRINT STUB"
        End If
    End If
    
    AvatarBG.Color = ShadeColor(Rnd(0xFF59C6CC, 0xFFF8D0CD))
    lblAvatar.Text = Avatar
    lblStatus.Text = csStatus
    lblEmpName.Text = EmpName
    lblDivision.Text = DivisionName
    
    Return p
End Sub
 

aeric

Expert
Licensed User
Longtime User
Try make it into 1 query instead of 2.
 
  • Like
Reactions: zed
Upvote 0
Top