How can I speed / tidy up this Sub?

markcrhome

Member
Licensed User
Longtime User
Hi, guys, getting closer to my first working app now!
Thanks to all the help on here, very much appreciated.

Basically my app contains no layouts.

The activity_create reads some data from a SQLlite database and populastes an array with details.

Then a tabview is added to the activity, and at the moment one tab is created.

I then call a sub to populate the tab page with firstly a scrollview, and then using a loop a number of of panels containing labels and images.
The code for filling thescrollview is as follows.
B4X:
Sub FillScrollViewTrucks

   'This has been designed to work on a device which is in portrait mode 
   'with a width of 600 and scale must have been 1 (160dpi)
   
   Dim bw As Float
   Dim voffset As Int

   Dim i As Int
   Dim xaxis As Int
   Dim sLastseen As String
   Dim sText As String
   Dim nstart As Int
   Dim nend As Int

   svTrucks.Panel.Height = 1280dip 'Make it bigger than screen, according to help i found!
   svTrucks.Height = 100%y
   svTrucks.Width = 100%x
   svTrucks.Panel.Color = Colors.Black
   svTrucks.Color = Colors.Black
   
   voffset = 0
   For i = 0 To 7

      pnlCounter(i).Initialize("")
      pnlCounter(i).Tag=i
      pnlCounter(i).SetBackgroundImage(bg)
      
'      Following commented out line works as expected on a 600 wide with scale 1      
'      svTrucks.Panel.AddView(pnlCounter(i),0,voffset,100%x,124)
      svTrucks.Panel.AddView(pnlCounter(i),0,voffset * 1dip,100%x,124dip)

      xaxis = poCtr(i).ctrtotal * xoffset ' move frame to correct x-axis pos according to its count
      imgFrame(i).Initialize("imgFrame")
      imgFrame(i).Tag = i
      imgFrame(i).Gravity = Gravity.CENTER
      If poCtr(i).ctrleading = 1 Then
         imgFrame(i).SetBackgroundImage(b1)
      Else
         imgFrame(i).SetBackgroundImage(b2)
      End If

'      Following commented out line works as expected on a 600 wide with scale 1
'      pnlCounter(i).AddView( imgFrame(i), xaxis, 40, 120, 84)
      pnlCounter(i).AddView( imgFrame(i), xaxis * 1dip, 40dip, 120dip, 84dip)

      Dim rs As RichString
      If poCtr(i).ctrlast  < 1 Then
         sLastseen = "Seen : Unknown"      
      Else
         DateTime.DateFormat = "HH:mm dd/MM/yy"
         sLastseen = "Seen : " & DateTime.Date(poCtr(i).ctrlast)
      End If
      sText = poCtr(i).ctrname & " (" & sLastseen & ")"
      nstart = 0
      nend = poCtr(i).ctrname.Length
      rs.Initialize(sText)
      rs.Style(rs.STYLE_BOLD, 0, nend - 1)
      rs.Color(Colors.white, nend, sText.Length)
      rs.Style(rs.STYLE_NORMAL, nend, sText.Length)
      rs.RelativeSize(.5, nend, rs.Length)
      lblName(i).Initialize("")
      lblName(i).Tag=i
      lblName(i).Text = rs
      lblName(i).TextColor = Colors.RGB(100,149,237)
      lblName(i).Typeface = Typeface.DEFAULT_BOLD
      lblName(i).Gravity = Gravity.TOP   
      lblName(i).TextSize=30

'      Following commented out line works as expected on a 600 wide with scale 1      
'      pnlCounter(i).AddView(lblName(i),0, 0, 500, 40)
      pnlCounter(i).AddView(lblName(i),0, 0, 500dip, 40dip)      

      imgAvatar(i).Initialize("imgAvatar")
      imgAvatar(i).Tag=i
      imgAvatar(i).Gravity = Gravity.CENTER
      imgAvatar(i).Bitmap = bp

'      Following commented out line works as expected on a 600 wide with scale 1
'      pnlCounter(i).AddView(imgAvatar(i), 530, 12, 64, 64)
      pnlCounter(i).AddView(imgAvatar(i), (lv.Width - 64dip)*1dip, 12dip, 64dip, 64dip)      
      
      lblTotal(i).Initialize("")
      lblTotal(i).Tag=i
      lblTotal(i).TextSize=38
      lblTotal(i).Text = poCtr(i).ctrtotal
      lblTotal(i).TextColor = Colors.White
      lblTotal(i).Typeface = Typeface.DEFAULT_BOLD
      lblTotal(i).Gravity = Gravity.CENTER_HORIZONTAL + Gravity.TOP
      
'      Following commented out line works as expected on a 600 wide with scale 1      
'      pnlCounter(i).AddView(lbltotal(i), xaxis , 46, 80, -2)
      pnlCounter(i).AddView(lbltotal(i), xaxis * 1dip, 46dip, 80dip, -2)
      voffset = voffset + 127dip

   Next

End Sub

This codes work well, in that it works!

In the code above the imgFrame() is an imageview which loads a picture of a truck. This has an associated longclick event.
In the longclick, I basical perform some database functions, and then call the sub FillScrollViewTrucks routine again to redraw the entire scrollview, as positions of the trucks will have changed, and indeed, the asscoiated icon etc will have.

What I want to know is should this routine first delete the svTrucks scrollview to clear up?

The routine may be called every couplke of seconds, so just wondering what effect it would have on resources running as it does currently.

If anyone can offer a better way to code the above section I would be forever grateful.

UPDATE!!!

I have just clicked the imgFrame imageview 30 times in succession. I have noticed now that the scrollview is very slow to scroll, but restarting means it scrolls very fast again.
 
Last edited:
Top