Android Question Output in ScrollView very slow

strupp01

Active Member
Licensed User
Longtime User
I currently want Output 413 data records in a scroll view. The number of records is steadily increasing. Approx. 12 seconds are required for the output. Is there a way to speed up the output or to program another type of output with a similar appearance?
The planned sample program with data is unfortunately too large, so only extracts from the programming. The code is repeated 413 times in a loop.

Sincerely
strupp01

B4X:
Sub Ausgaben_aus_DB_lesen
    Dim T1 As String
    DateTime.TimeFormat = "HH:mm:ss"

    T1 = DateTime.TimeParse(DateTime.Time(DateTime.Now))

    Cursor97 = Starter.SQL_DB.ExecQuery("SELECT Ident_Nr, Finanzverlauf_Name, Konto, Summe_Finanzverlauf, Datum, Datum_zum_sortieren, Kategorie, Beschreibung, Text_Waehrung_Abkuerzung, Speicherpfad_Icon FROM Finanzverlauf")' order by Datum_zum_sortieren, Ident_Nr DESC")

    ScrollView_Finanzverlauf.Panel.Width = 780dip
    ScrollView_Finanzverlauf.Panel.Height = 82dip+((Cursor97.RowCount-1)*56dip)' +10dip

    If ScrollView_Finanzverlauf.Panel.Height < 1000dip Then
        ScrollView_Finanzverlauf.Panel.Height = 1000dip
    End If
    
    For i=1 To Cursor97.RowCount
        Cursor97.Position = i-1

        'IconButton für Kont0 erzeugen
        KontoIcon.Initialize("KontoIcon")
        
        Dim Button_Konto_Icon As BitmapDrawable
        Button_Konto_Icon.Initialize(LoadBitmap(File.DirAssets, "Muenzen_45x30.png"))
    
        KontoIcon.IconPadding = 0    '350   'geändert
        KontoIcon.setIcon(True,Button_Konto_Icon)
        
        Dim c As ColorDrawable
        c.Initialize2(Colors.White,5dip,3dip,Colors.White)
        KontoIcon.Background = c
        
        
        ScrollView_Finanzverlauf.Panel.AddView(KontoIcon, 15dip, 8dip+(Cursor97.Position*56dip), 80dip, 50dip)


        'Textzeilen der Ausgaben ohne Geldbetrag
        '1.Zeile
        Verschiedene_Ausgaben_Zeile1.Initialize("Verschiedene_Ausgaben_Zeile1")
        Dim c As ColorDrawable
        c.Initialize2(Colors.White,5dip,3dip,Colors.Transparent)
        Verschiedene_Ausgaben_Zeile1.Background = c
            
        Verschiedene_Ausgaben_Zeile1.Text = Cursor97.GetString("Finanzverlauf_Name")
        Verschiedene_Ausgaben_Zeile1.Tag = i
        Verschiedene_Ausgaben_Zeile1.TextColor = Colors.Black
        Verschiedene_Ausgaben_Zeile1.Typeface = Typeface.DEFAULT
        Verschiedene_Ausgaben_Zeile1.Gravity = Bit.Or(Gravity.LEFT, Gravity.BOTTOM)
        Verschiedene_Ausgaben_Zeile1.TextSize =16
        ScrollView_Finanzverlauf.Panel.AddView(Verschiedene_Ausgaben_Zeile1, 100dip, 5dip+(Cursor97.Position*56dip), 660dip, 40dip)
            
        'Textzeilen  Ausgabekategorien
        '2.Zeile
        Verschiedene_Ausgabekategorien_Zeile2.Initialize("Verschiedene_Ausgaben_Zeile2")
        Dim c As ColorDrawable
        c.Initialize2(Colors.White,5dip,3dip,Colors.Transparent)
        Verschiedene_Ausgabekategorien_Zeile2.Background = c
            
        Verschiedene_Ausgabekategorien_Zeile2.Text = Cursor97.GetString("Kategorie")
        Verschiedene_Ausgabekategorien_Zeile2.Tag = i
        Verschiedene_Ausgabekategorien_Zeile2.TextColor = Colors.Red    '.ARGB(255,31,122,31)    '(255,26,101,26)    '(255,21,81,21)
        Verschiedene_Ausgabekategorien_Zeile2.Typeface = Typeface.DEFAULT
        Verschiedene_Ausgabekategorien_Zeile2.Gravity = Bit.Or(Gravity.LEFT, Gravity.BOTTOM)
        Verschiedene_Ausgabekategorien_Zeile2.TextSize =16
        ScrollView_Finanzverlauf.Panel.AddView(Verschiedene_Ausgabekategorien_Zeile2, 100dip, 30dip+(Cursor97.Position*56dip), 660dip, 38dip)
            
        '        'Textzeile der Verschiedene_Ausgaben_Werte nur Geldbetrag Zeile1
        Verschiedene_Ausgaben_Werte.Initialize("Verschiedene_Ausgaben_Werte")
        Dim c As ColorDrawable
        c.Initialize2(Colors.White,5dip,3dip,Colors.Transparent)
        Verschiedene_Ausgaben_Werte.Background = c
            
        Verschiedene_Ausgaben_Werte.Text = "-" & NumberFormat2(Cursor97.GetDouble("Summe_Finanzverlauf"), 1, 2, 2, False) & " " & Cursor97.GetString("Text_Waehrung_Abkuerzung")
        Verschiedene_Ausgaben_Werte.Tag = i
        Verschiedene_Ausgaben_Werte.Color = Colors.Transparent
        Verschiedene_Ausgaben_Werte.TextColor = Colors.Red    '.ARGB(255,31,122,31)
        Verschiedene_Ausgaben_Werte.Typeface = Typeface.DEFAULT_BOLD
        Verschiedene_Ausgaben_Werte.Gravity = Bit.Or(Gravity.RIGHT, Gravity.BOTTOM)
        Verschiedene_Ausgaben_Werte.TextSize =16
        ScrollView_Finanzverlauf.Panel.AddView(Verschiedene_Ausgaben_Werte, 100dip, 5dip+(Cursor97.Position*56dip), 660dip, 40dip)
            
        'Textzeile der Verschiedene_Ausgaben_Datum nur Datum Zeile2
        Verschiedene_Ausgaben_Datum.Initialize("Verschiedene_Ausgaben_Datum")
        c.Initialize2(Colors.White,5dip,3dip,Colors.Transparent)
        Verschiedene_Ausgaben_Datum.Background = c
            
        Verschiedene_Ausgaben_Datum.Text = Cursor97.GetString("Datum")
        Verschiedene_Ausgaben_Datum.Tag = i
        Verschiedene_Ausgaben_Datum.Color = Colors.Transparent
        Verschiedene_Ausgaben_Datum.TextColor = Colors.Black
        Verschiedene_Ausgaben_Datum.Typeface = Typeface.DEFAULT
        Verschiedene_Ausgaben_Datum.Gravity = Bit.Or(Gravity.RIGHT, Gravity.BOTTOM)
        Verschiedene_Ausgaben_Datum.TextSize =16
        ScrollView_Finanzverlauf.Panel.AddView(Verschiedene_Ausgaben_Datum, 100dip, 30dip+(Cursor97.Position*56dip), 660dip, 38dip)
            
        'Transparentes Label ohne Text, nur für anklicken und somit starten der Transaktion
        Transparenter_Button.Initialize("Transparenter_Button")
        c.Initialize2(Colors.Transparent,5dip,3dip,Colors.Transparent)
        Transparenter_Button.Background = c
            
        Transparenter_Button.Text = ""
        Transparenter_Button.Tag = i
        Transparenter_Button.Color = Colors.Transparent
        Transparenter_Button.TextColor = Colors.Black
        Transparenter_Button.Typeface = Typeface.DEFAULT
        Transparenter_Button.Gravity = Bit.Or(Gravity.RIGHT, Gravity.BOTTOM)
        Transparenter_Button.TextSize =12
        ScrollView_Finanzverlauf.Panel.AddView(Transparenter_Button, 3dip, 7dip+(Cursor97.Position*56dip), 760dip, 56dip)
            
        Canvas1.Initialize(Transparenter_Button)
        Canvas1.DrawLine(20dip, 54dip, 750dip, 54dip, Colors.Black, 2dip)

        Sleep(0)

    Next
 

klaus

Expert
Licensed User
Longtime User
Approx. 12 seconds ...
Seems quite long to me.
Did you test it in Debug or in Release mode?

A few comments:
1. This code must be outsides the For / Next loop.
B4X:
Dim Button_Konto_Icon As BitmapDrawable
Button_Konto_Icon.Initialize(LoadBitmap(File.DirAssets, "Muenzen_45x30.png"))
You load for each item the same bitmap, this takes much time.

2. This code looks strange to me:
B4X:
    For i=1 To Cursor97.RowCount
        Cursor97.Position = i-1

        'IconButton für Kont0 erzeugen
        KontoIcon.Initialize("KontoIcon")
Where do you Dim KontoIcon ?
This must be done insides the For / Next loop! To generate a new instance for each item.
It's the same for the other views.

3. What is this code supposed to do?
B4X:
Canvas1.Initialize(Transparenter_Button)
Canvas1.DrawLine(20dip, 54dip, 750dip, 54dip, Colors.Black, 2dip)
Initializing a Canvas for each Button could lead to memory problems.
Not sure if this also takes time.

4. Move the Sleep(0) after the For / Next loop.
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Hello Claus,
Time in release mode
1. Are just sample data. Each line can have a different icon. Total about 15 icons
2. Do I have to test
3.I tried outside. Not faster, but problems with large ScrollView's
4.I laid it out so that the first lines are already visible on the tray and I can partially scroll
 
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
@strupp01 as far as I'm concerned, the simplest and quickest solution is to use xCustomListView with lazy loading. Thus you can load long continuous lists of data within a second or two and that includes icons/images, device speed dependent.

Can you post a screenshot of what you are trying to achieve?

xCLV can easily be created as a list with images, example below.

0_Resizer_15809467492850.png
 
Last edited:
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
@Peter Simpson, Sorry I'm only answering now. Was already sleeping yesterday.
Here is the screenshot .
1580985423877.png


The 4 output fields per output are to be placed exactly like this (2 right-justified, 2 left-justified)
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
@Peter Simpson, Sorry I'm only answering now. Was already sleeping yesterday.
Here is the screenshot .
View attachment 88302

The 4 output fields per output are to be placed exactly like this (2 right-justified, 2 left-justified)

Basically the same as my screenshot but without a border and elevation. xCLV (xCustomListView) as @Erel said :)

Enjoy...
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Today I switched my program to CustomListView as a test. The result is disastrous.
For 3000 records about 40 seconds in release mode . Am I doing something wrong?
Here is the code:
B4X:
Sub Ausgaben_aus_DB_lesen_neu

    Cursor97 = Starter.SQL_DB.ExecQuery("SELECT Ident_Nr, Finanzverlauf_Name, Konto, Summe_Finanzverlauf, Datum, Datum_zum_sortieren, Kategorie, Beschreibung, Text_Waehrung_Abkuerzung, Speicherpfad_Icon FROM Finanzverlauf")' order by Datum_zum_sortieren, Ident_Nr DESC")

   
    For i=1 To Cursor97.RowCount
        Cursor97.Position = i-1

        CustomListView_Finanzverlauf.Add(CreateListItem1(Cursor97.GetString("Finanzverlauf_Name").Trim, 10dip, 70dip),$"Item #${i}"$)

    Next

End Sub

Sub CreateListItem1(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    p.SetLayout(0, 0, Width, Height)
    p.LoadLayout("CellItem")
    Label1.Text = Text.Trim
    Label2.Text = Cursor97.GetString("Kategorie").Trim
    Label3.Text = "-" & NumberFormat2(Cursor97.GetDouble("Summe_Finanzverlauf"), 1, 2, 2, False) & " " & Cursor97.GetString("Text_Waehrung_Abkuerzung")
    Label4.Text = Cursor97.GetString("Datum").Trim
   
    Return p
End Sub
 

Attachments

  • 1581018875395.png
    1581018875395.png
    27.3 KB · Views: 234
Last edited:
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Wow, you are doing something wrong for sure @strupp01.

1. First you need to start with this post. Create your layout and get everything working.

Once you have got your layout up and running, you then need to read and integrate lazy loading from the following post.

You should be able to populate your list in well under 1 second. Just start with the first link then move onto the second link.
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Thanks again to both. Since I don't speak English and translate everything by computer, I probably didn't understand some things correctly. Will take it easy again, since more insights have been noticed by me. 😓
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Thanks again to both. Since I don't speak English and translate everything by computer, I probably didn't understand some things correctly. Will take it easy again, since more insights have been noticed by me. 😓
What is your language?
1581066808243.png


I always hope, but in vain, that members write it in their profile - or rather write their "Location" ("Account details").

1581067080836.png


Maybe there is already a forum for your language.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Well it's all done and dusted, and here is a screenshot for you.
Your code was completed about an hour ago, but I've been in the phone to a customer. I've PM you the xCLV with lazy loading code working with your database and here is a current screenshot taken a few minutes ago. It all loads up in about half a second and the active filter search works quickly too.

You should study the source code and hopefully you will learn how it works.
Screenshot_20200207-112032.png


Enjoy...
 
Upvote 0
Top