Android Question Searchtemplate large lists

harinder

Active Member
Licensed User
Longtime User
I am using below code to add 7000 airport choices into a Searchtemplate from a database. It takes 2-3 secs for searchtemplate to be loaded with the choices. Any faster method? Thanks
B4X:
Sub airportquery
    Dim rs As ResultSet= Main.SQL1.ExecQuery( "SELECT airport FROM " & Main.DBTableplaces & " ORDER BY airport ")
    Dim mylist as list
    mylist.initialize
    Do While rs.NextRow
        mylist.Add(rs.GetString("airport"))       '7000 airport rows
    Loop
    SearchTemplateairport.SetItems(mylist)
    rs.close
End Sub
 

harinder

Active Member
Licensed User
Longtime User
Can someone help with the code for progress bar to show for the busy time until the searchtemplate loads? My project above refers..thnx
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
   
    Activity.LoadLayout("cities")
    SearchTemplateairport.Initialize
    base = Activity
    ProgressBar1.Visible=False
    Dialog.Initialize (base)
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub btnfrom_click
   ProgressBar1.Visible=True
    Sleep(0)
    progr
End Sub

Sub progr
    airportquery
    wait for (Dialog.ShowTemplate(SearchTemplateairport, "", "", "CANCEL")) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Dim airport As String =SearchTemplateairport.Selecteditem
        btnfrom.xLBL.Text = airport.substring2(0,3)
    End If
End Sub



Sub airportquery

    Dim t1 As Long=DateTime.Now
    Dim cities As List = File.ReadList(File.DirAssets, "Airports.txt")
    Dim t2 As Long=DateTime.Now
    Dim dt As Long=t2-t1
    Log($"time to create the list ${dt}"$)
    SearchTemplateairport.SetItems(cities)
    ProgressBar1.Visible=False
End Sub
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
B4X:
    anotherprogressbar1.Visible=True
    Sleep(1000)
    wait for (Dialog.ShowTemplate(SearchTemplateairport, "", "", "CANCEL")) Complete (Result As Int)
    anotherprogressbar1.Visible=False
This works better as animation of progressbar is visible for 1 sec for user to see
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I did it exactly as I previously wrote and the list appears immediately.

It is true that it takes it 2-3 seconds to load the serialized index, however you only need to do it once and it happens in the background so the user will not notice it at all. It will not freeze your app like when you call SetItems.

This is the code:
B4X:
Sub Globals
    Private xui As XUI
    Private Dialog As B4XDialog
    Private base As B4XView
    Private btnfrom As SwiftButton
    Private SearchTemplateairport As B4XSearchTemplate
    Private index As Object
End Sub

Sub Activity_Create(FirstTime As Boolean)
  
    Activity.LoadLayout("cities")
    SearchTemplateairport.Initialize
    base = Activity
    Dialog.Initialize (base)
    btnfrom.Enabled = False
    Dim ser As B4XSerializator
    ser.ConvertBytesToObjectAsync(File.ReadBytes(File.DirAssets, "index"), "ser")
    Wait For ser_BytesToObject (Success As Boolean, NewObject As Object)
    index = NewObject
    btnfrom.Enabled = True
End Sub

Sub btnfrom_click
    SearchTemplateairport.SetIndex(index)
    wait for (Dialog.ShowTemplate(SearchTemplateairport, "", "", "CANCEL")) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Dim airport As String =SearchTemplateairport.Selecteditem
        btnfrom.xLBL.Text = airport.substring2(0,3)
    End If
End Sub

B4J code:
B4X:
Dim search As B4XSearchTemplate
search.Initialize
Dim index As Object = search.SetItems(File.ReadList(File.DirAssets, "Airports.txt"))
Dim ser As B4XSerializator
File.WriteBytes("C:\Users\H\Downloads\index", "", ser.ConvertObjectToBytes(index))
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
Thank you Erel..you have helped to reduce the searchtemplate loading time to 1 sec?...just curious to know that why is the index file size 2.4 mb ? Thank you for your help:D
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
.just curious to know that why is the index file size 2.4 mb
The index is a collection that maps between the many possible prefixes and substrings and the values. It gets quite large. This is the reason that it took 2 seconds to build the index.
 
Upvote 0

harinder

Active Member
Licensed User
Longtime User
Amongst the three, viz searchtemplate(both with and without b4xserializator), searchview and Xclv with lazy loading, I find that xclv clearly is a notch above. Searchtemplate lags if items >500(as rightly set as default by Erel). With b4x serializator, definitely the list loading time comes down considerably(thanks to help by Erel:)). Searchview does not display all items until you start typing and then the filter works.
But xclv is totally hands down, thanks to Erel for its implementation. As I was struggling for a viable solution, I approached Peter Simpson and he was very kind to help me out. I now use searchtemplate for short search requirements <500 and for larger search lists I use xclv with lazy loading:cool:
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I'm pleased that you've finally got the solution that you were looking for.
But as you're only reading airport names from the database for your app, you should only be requesting data from that one column and not the whole table. You should optimise the query by cutting and pasting the following 5 lines over the top of the lines in your code. Well, not unless you are in fact going to read every column in the `airports` table.
Only read 'airports' column in your sql query:
    If TxtSearchFilter.Text.Length = 0 And CLVAirports.Size = 0 Then
        SenderFilter = SQL1.ExecQueryAsync("SQL", "SELECT `airport` FROM Airports ORDER BY airport ASC", Null)
    Else
        SenderFilter = SQL1.ExecQueryAsync("SQL", $"SELECT `airport` FROM Airports WHERE airport  Like '%${New}%' ORDER BY airport ASC"$, Null)
    End If
If you don't already know about it, you should also learn about Smart String Literal to use within your sql queries.

Also you DO NOT need the progress bar, your database loads up a lot quicker without it. Don't forget that the example I gave you fully loads up in way under a second.

Enjoy...
 
Last edited:
Upvote 0
Top