Android Example Async task help

Discussion in 'Tutorials & Examples' started by Gianni4Forum, Mar 30, 2015.

  1. Gianni4Forum

    Gianni4Forum New Member

    Hi expert
    i'm trying B4A coming from Appcelerator.
    It is very fast and simply

    I try to use a scrollview populated from a sqllite db cursor with many records
    my scrollview is very slow when loading

    For this reason I'm thinking of asynchronous code or other ways

    can someone help me please
    thank's


    the above is my code
    Sub AR_Load
    Dim ArBitmap As Bitmap
    Dim MainPanel As Panel
    Dim PanelTop, PanelHeight As Int
    Dim progress As Int
    Dim Cur as Cursor
    Dim Db as SQL
    PanelTop = 0
    MainPanel = SV_Ar.Panel
    MainPanel.Color = Colors.LightGray
    db.Initialize(File.DirAssets,"db_sede.db",True)

    cur = db.ExecQuery("Select cdar, ar_descrizione from ar")
    ' this return about 7000 records and is fastest (250 ms)

    For i = 0 To 500 ' cur.RowCount - 1 '(only fist 500 records for test)
    cur.Position = i
    progress = i / 500 * 100
    Dim ItemPanel As Panel
    Dim ImgAr As ImageView
    Dim LblCdAr, LblArDescrizione As Label
    LblCdAr.Initialize("")
    ArBitmap.Initialize(File.DirAssets,"TREND.png")
    ItemPanel.Initialize("")
    LblArDescrizione.Initialize("")
    PanelHeight = 90dip
    MainPanel.AddView(ItemPanel,5dip,PanelTop,SV_Ar.Width,PanelHeight)
    ItemPanel.Color = Colors.Black
    ItemPanel.AddView(LblCdAr,80dip,8dip,240dip,PanelHeight)

    LblCdAr.Color = Colors.Black
    LblCdAr.Tag = LblCdAr
    LblCdAr.Text = cur.GetString("cdar")
    LblCdAr.TextSize = 20
    ItemPanel.AddView(LblArDescrizione,80dip,30dip,240dip,30dip)
    LblArDescrizione.TextSize = 16
    LblArDescrizione.Tag = LblArDescrizione
    LblArDescrizione.Text = cur.GetString("ar_descrizione")
    ImgAr.Initialize("")
    '
    ItemPanel.AddView(ImgAr,1dip,5dip,75dip,75dip)
    ImgAr.Bitmap = ArBitmap
    ImgAr.Gravity = Gravity.FILL
    PanelTop = PanelTop + PanelHeight + 1dip
    ProgressBar.progress = progress
    If i Mod 50 = 0 Then ' refresh progressbar every 50 record
    DoEvents
    End If
    Next
    MainPanel.Height=PanelTop
    cur.Close
    Msgbox("","End Load scrollview")
    End Sub
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    The first step is to remove the DoEvents call.
    You should avoid loading the image multiple times. Load it once and reuse it.

    Make sure to test it on a real device and test performance in Release mode.
     
  3. Gianni4Forum

    Gianni4Forum New Member

    Hi Erel
    thank's for your answer
    I'm starting with B4a only yesterday.

    that i want is a list of product with some product information and product icon
    i want insert some filter and need to display result in a listview or scrollview

    I imagined that DoEvents would slow the program but I have inserted it to obtain a progressbar refresh and increment because without the above code
    Code:
    If i Mod 50 = 0 Then ' refresh progressbar every 50 record
    DoEvents
    End If
    I did not see any movement of it
    In addition I have used several times
    Code:
    ArBitmap.Initialize( ...
    because in my code the name of icon is different for all product

    I noticed from forum that you are a real expert, could you give me some help please?
    thanks
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Based on the code you posted you are loading the same image multiple times. Anyway, did you test it in release mode on a real device?
     
  5. Gianni4Forum

    Gianni4Forum New Member

    both on real and emulator!
    Can you help me how to reuse img please? i don't know how to do that
    thank's


    Can you have an Example of Async Task ?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Dim ArBitmap As Bitmap = LoadBitmap(...)

    For i = 1 to 500
      
    'here you can use ArBitmap multiple times
    Next
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice