Android Question Layout is taking time to show

Shay

Well-Known Member
Licensed User
Longtime User
Strange one:

I have activity Main, I have button which do
Activity.finish
StartActivity (MainMenu)

on MainMenu activity_Create I first load the layout
Activity.LoadLayout("StepByAddress")

In this Activity I have AutoCompleteEditText1
which loads large list from local database

So I did:
ProgressDialogShow2("Loading, just a sec",False)
ReadTable (this read the local database and insert to list)

the problem is:
Once I click the button on Main activity, it will not show the layout and not show the progressdialog
until the "ReadTable" will finish, so if someone click on the button in Main, it looks like app not responding for few seconds instead of loading the layout and saying just a sec

If I remark the "ReadTable" all is good
 

Mahares

Expert
Licensed User
Longtime User
Did you try to add a DoEvents when you populate the list like this to see if it helps?
B4X:
For i=0 To Cursor1.RowCount-1
  Cursor1.Position = i
  lst.Add(Cursor1.Getstring("Shay")....... 
  If i Mod 50 = 0 Then DoEvents   'you can change the 50 to some other number
Next
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
No I didn't since the odd thing is why the layout is not coming up
since it written before ReadTable, so why it is jumping to ReadTable and "Skipping" LoadLayout and ProgressDialogShow
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You should try the DoEvents and see what happens.
DoEvents
Processes waiting messages in the messages queue.
DoEvents can be called inside lengthy loops to allow the program to update its UI.
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
SQL.ExecQueryAsync did not do the job
DoEvent is much better
I also reduced the list size by better executing query

Thanks
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
22,000 - all the streets in Israel DB
 

Attachments

  • israel.zip
    500.1 KB · Views: 157
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
B4X:
Sub ReadTable

    Dim Cursor1 As Cursor
    Dim Street As String
    Dim i As Int
       
    ListStreet.Clear
   
    If SQL1.IsInitialized = False Then
     SQL1.Initialize(File.DirInternal ,"israel.db", False)
    End If

    Cursor1=SQL1.ExecQuery("SELECT DISTINCT Street FROM Israel")
    Cursor1.Position=0
   
    For i=0 To Cursor1.RowCount-1
     Cursor1.Position = i
     Street = Cursor1.GetString("Street")
     ListStreet.Add(Street)
''     If i Mod 50 = 0 Then DoEvents
    Next

    Cursor1.Close
    SQL1.Close
   
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
it will be fun to find your street in that list :)

why not populate the list after 3 chars have been entered?
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
since I wanted to load the list before client type, so it will be fast for the user
but I can try this
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
If you let the user first enter three characters the loading time will be increased a lot instead of loading the hole list
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
1. I created a small project to load all streets in a list and streamlined some of your code. I was able to load all 20751 records to a list and go to the last record in the list in only 1 ½ second total. See code below:

2. When I loaded all 41653 records by removing the DISTINCT from the query, it took 2 seconds on a Galaxy Tab2 tablet. Your cities and street names are in Hebrew. Sorry I do not speak or read Hebrew.

B4X:
Sub Globals
    Dim DBFileName As String = "israel.db"
    Dim DBFilePath As String   
    Dim Cursor1 As Cursor
    Dim ListStreet As List
    Dim lblSec As Label  'located in the layout StepByAddress
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("StepByAddress")
    If File.ExternalWritable Then 
        DBFilePath = File.DirRootExternal 
    Else 
        DBFilePath = File.DirInternal 
    End If
   
    If Not(File.Exists(DBFilePath,DBFileName)) Then
        File.Copy(File.DirAssets,DBFileName,DBFilePath,DBFileName)
    End If
   
    If SQL1.IsInitialized =False Then
        SQL1.Initialize(DBFilePath,DBFileName,True)
    End If
    ListStreet.initialize
   
    ProgressDialogShow2("Loading, just a sec",False)
    ReadTable
End Sub

Sub ReadTable
    Dim t0 As Long= DateTime.Now
    Cursor1=SQL1.ExecQuery("SELECT DISTINCT Street FROM Israel")
   
    For i=0 To Cursor1.RowCount-1
     Cursor1.Position = i
      ListStreet.Add(Cursor1.GetString("Street"))
    Next
   
    Dim t1 As Long = DateTime.Now
    lblSec.Text = Round2((t1 - t0)/1000,1)
    lblSec.Text =lblSec.Text & " seconds"
    ProgressDialogHide
    Log("Last record in list: " & ListStreet.Get(ListStreet.Size-1))
    Log("Time elapsed: " & lblSec.Text)
    Log("Number of recs: " & Cursor1.RowCount)
    Cursor1.Close
    SQL1.Close   
End Sub
 
Upvote 0
Top