Collection example

Robto

Member
Licensed User
Longtime User
Hi guys,

I've been searching the forum for days now to come with a way to achieve the following: I want 5 buttons to change visibility (via a timer) in a controlled sequence.

I believe the Collection class is the best solution to achieve this. Basic4android - Collections (Core)

Theory:
I have to put 5 buttons in an dynamic array, add a timer sub and a static value for the timer to increase. The ticker_tick adds +1 to the static value. Based on the static.value a button from the array is 'picked' and visibility is changed to TRUE for that button. Next tick all buttons visibility is FALSE again and static.value is increased +1 and next button visibility will be picked and changed to TRUE, etc, etc

Am I on the right track or is there a simpler way to achieve this?

I need an example how to add buttons in a Collection and an example how to pick a button from that array and change the visibility on that but I couldn't find anything to suit my needs :BangHead:

Can you please point me to an example? Or something I can recycle to my needs? :sign0163:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
'Activity module
Sub Process_Globals
   Dim timer1 As Timer
End Sub

Sub Globals
   Dim buttons As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      timer1.Initialize("Timer1", 1000)
   End If
   buttons.Initialize
   For i = 0 To 4
      Dim b As Button
      b.Initialize("b")
      b.Text = i
      Activity.AddView(b, 10dip, 80dip * i, 100dip, 50dip)
      buttons.Add(b)
   Next
End Sub

Sub Timer1_Tick
   Dim i As Int = Rnd(0, buttons.Size)
   Dim b As Button = buttons.Get(i)
   b.Visible = Not(b.Visible)
End Sub

Sub b_Click
   Dim b As Button = Sender
   b.Color = Colors.RGB(Rnd(0, 256), Rnd(0, 256), Rnd(0, 256))
End Sub


Sub Activity_Resume
   timer1.Enabled = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   timer1.Enabled = False
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…