B4J Question creating control array at runtime

a n g l o

Active Member
Licensed User
Longtime User
hello, this is an example for the way i know how to create a control array in b4j.
if i would'nt want a gui update during it - that's fine.
but since i do want a gui update during action - i add sleep(0)+Resumable Sub.
that slows the action from 1 sec to 7 sec (when running the executable created by 'build standalone package') on the test laptop.
is there a way to speed up this code ?

B4X:
Sub Process_Globals
            Private fx As JFX
            Private MainForm As Form
            Private offsetX As Double
            Private offsetY As Double
End Sub

Sub AppStart (Form1 As Form, Args() As String)
            MainForm = Form1
            MainForm.RootPane.LoadLayout("Layout1")
            maximizeMainForm
            MainForm.Show
            offsetX=MainForm.Width/2000/1.5
            offsetY=MainForm.Height/2000/1.5
End Sub

Private Sub maximizeMainForm
            Dim mf As JavaObject = MainForm
            mf.GetFieldJO("stage").RunMethod("setMaximized", Array(True))
End Sub

Sub MainForm_MouseClicked (EventData As MouseEvent)
            Wait For (loadControlArray) Complete (result As String)
End Sub

Private Sub loadControlArray As ResumableSub
            Dim t0 As Long
            t0=DateTime.Now
            Dim i As Int
            Dim x As Double
            Dim y As Double
            x=1
            y=1           
            For i=1 To 2000
                    Dim lbl As Label
                    lbl.Initialize("lbl")
                    lbl.Text="X"
                    lbl.Tag=i
                    MainForm.RootPane.AddNode(lbl,x,y,10dip,10dip)
                    x=x+offsetX
                    y=y+offsetY       
                    Sleep(0)     
            Next
            fx.Msgbox(MainForm, NumberFormat((DateTime.Now-t0)/1000,0,0) & " seconds.","")
            Return Null
End Sub
 

a n g l o

Active Member
Licensed User
Longtime User
thanks. i'm actualy using it. this is just an example with the lightest view possible.
i was hoping to get an answer like : "this is not the right way to create control array. there is a
better and faster way".....a different approach. something like in vb6 maybe.
 
Upvote 0

a n g l o

Active Member
Licensed User
Longtime User
look at this for example (zoom fully out) :
suppose it's not a web but a desktop standalone.
suppose you have all the data needed already loaded in mem.
all you do is to create the controls ( it's all controls/views . isn't it ?)
i believe there are tousandnds of them.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
1) Every time you click your mouse, your create all the labels. Should you not create the labels once, and then only update the pertinent information?
2) Would XUI2D be a better fit for something like this?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
1) Every time you click your mouse, your create all the labels. Should you not create the labels once, and then only update the pertinent information?
I guess that's just a test and he'll only click once (sort of initialization).

In the case of the linked example ("Live flight tracker"), he will have an array of custom types (I hope :)) for each plane, with the current coordinates and the view (ImageView?) and will use SetLayoutAnimated to move each plane, in a loop on the Array)
 
Upvote 0

a n g l o

Active Member
Licensed User
Longtime User
ok. i may not explained what i'm after clearly.
the above loop does not really create an array of labels. it just adds labels to to node collection.
it's true that they all share events where the sender is identified. BUT i'm after a real array
where you can code : lbl(1429).Text="is it possible ?"
Thank you
 
Upvote 0

a n g l o

Active Member
Licensed User
Longtime User
sorry, now i see that i forgot to mention that i want to create the control array dynamically at runtime.
i put the number 2000 in the for loop just for the example. actually it's a value that known
only at run time.
so thanks klaus, but it seems that the DIM have to be inside the loop and added to exsiting array.
(as in vb6 when you create index 0 control at design time, visible=False, and then use load statement at runtime
to load more into same array)
 
Upvote 0

a n g l o

Active Member
Licensed User
Longtime User
thank you klaus ! first thing - it gives me the option to get to a control by its index and that what i've asked for.
i've forgotten that b4j list can get objects. You reminded me that and thanks again !
if nothing more elegant will come out - that will do.
i mentioned 'elegant' because still i can't do : list1.Get(1545).Text="someting". i have to assign the list1.Get(1545)
to an object (label in this case), and only then read/write to it. that's why it will do, but hoping for something more elegant....
 
Upvote 0

a n g l o

Active Member
Licensed User
Longtime User
klaus : i'll never do such thing. i started coding under DOS when there was only 640k mem on the machine...
i mentally can't consciously use unnecassry memory....
LucaMs : i'm not familiar with that. i've looked at it shortly and got a basic impression that it's all around collections.
my controls are already belong to collection (of their container node) where i can 'for each' them if i need.
of course, i may got the wrong impression. i put a note to myself to look at it deeply when i have the time.
Thank you
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Using array instead of list because of the reason mentioned above is a very non-elegant solution.
Same is true for worrying about a few extra bytes. A single bitmap that you load to your app will consume 1000 times more memory than this collection.

I usually avoid joining "preoptimization" discussions as they are completely fruitless.

Here is a solution for the complex task of getting a view from a list:
B4X:
Sub GetView(Index As Int) As B4XView
 Return views.Get(index)
End Sub
 
Upvote 0
Top