B4J Tutorial [ABMaterial] AddArrayComponent (simple example)

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
 

rbirago

Active Member
Licensed User
Longtime User
The AddArrayComponent is a good method to understand which element is clicked, but my problem is different. In my container there is not a clickable component like a button, it is composed by various labels. I need a system to manage the click-event on the element itself, like the element of an ABMList. I could add a button on each element, but the standard user is compelled to click everywhere in an element to select it. So the problem: How to catch a click-event on a cell? Or: Is there a clickable component (like a pane) in which I can insert my container labels?
 

MichalK73

Active Member
Licensed User
Longtime User
You can catch a click on a list box.
When adding a new ABMList value, you specify the 'returnId' returned, which is sent to the procedure handling a click on a given list field.

B4X:
    ....
    Dim lista As ABMList
    lista.Initialize(page, "lista", ABM.COLLAPSE_ACCORDION, "")
    For idx=1 To 100
        lista.AddItem("x_"&idx, ABMShared.BuildSizeH5(page, "id"&idx, Rnd(10000,100000)))
    Next
    page.Cell(2,1).AddComponent(lista)
    ....
   
Sub lista_Clicked(ItemId As String)

    Dim p() As String = Regex.Split("_", ItemId)
    Log("idx->"&p(1))
End Sub

That's it in a minute.
 
Top