B4J Tutorial [ABMaterial] AddArrayComponent (simple example)

Harris

Expert
Licensed User
Cat Skinning - there is more than one way....

I wanted to create a configuration sheet using ABMSwitch. However, the click method does not have a (Target as String) param. Same goes for ABMCheckbox... ABMChips was used for this example since it works.

I found the original example of AddArrayComponent showing its' use in a complex fashion, and lacking details of the click method.

Basically, we have many chips (or switches / check boxes when ever they support the target param) to select from (turn ON / OFF). A button on the main form presents this ModalSheet. Using AddArrayComponent we can determine which item was selected - and toggle it.

chips.png


The following code creates this sheet and will select / deselect any item.

B4X:
Sub btnpids_Clicked(Target As String)

    Dim pidbox As ABMModalSheet = page.ModalSheet("pidbox")
    Dim j As Int = 1
    Dim k As Int = 2
  
    Dim sql As SQL = DBM.GetSQL
    Dim users As List = DBM.SQLSelect(sql, "SELECT * FROM ecmpids order by pid")
    If users.Size > 0 Then
        For i = 0 To users.Size -1
            Dim user As Map = users.Get(i)
            Dim id,disp As Int
            Dim nam As String
            Dim switch1 As ABMChip  
          
            id = user.Get("pid")
            nam = user.Get("description")
            disp = user.Get("display")

            If id <> 1 Then    ' datetime value used for x grid on chart/graph
                switch1.Initialize(page, ""&id , nam, False,  "")

                If disp = 1 Then
                    switch1.Image = "../images/star.png"
                Else
                    switch1.Image = "../images/ostar.png"
                End If

                switch1.Tag = disp
                pidbox.Content.Cell(k,j).AddArrayComponent(switch1, "pid")
              
                j = j + 1
                If j = 5 Then
                    k = k + 1
                    j = 1
                End If

            End If
        Next  
    End If
    DBM.CloseSQL(sql)
    page.Refresh
    page.ShowModalSheet("pidbox")
  
End Sub

Sub pid_Clicked(Target As String)

   Dim pidbox As ABMModalSheet = page.ModalSheet("pidbox")
   Dim switch1 As ABMChip = pidbox.Content.Component(Target)  'Target is the selected component

'   Log("what is component: "&Target&"   id: "&switch1.ID&"   Tag: "&switch1.Tag )

   If switch1.Tag = 1 Then       ' toggle the image and set the tag property accordingly
      switch1.Image = "../images/ostar.png"
      switch1.Tag = 0
   Else
      switch1.Image = "../images/star.png"
      switch1.Tag = 1
   End If        

   switch1.Refresh
  
End Sub
 
Top