Android Question Individual Clickable words in listview

Tim Chapman

Active Member
Licensed User
Longtime User
I want to put individual clickable words in a listview. My code for making the listview is here:
B4X:
Sub ItemView(ListOfMaps As List, ListHeader As List, JustificationMap As Map)
  
    Dim View As ListView
    Dim cs As CSBuilder : cs.Initialize()
    cs.Color(Colors.Black)
    Dim ListHeaderLabel As Label
    Dim Label1 As Label
    Dim PaddedRow As Map
  
    Dim ItemMap As Map
    Dim LengthsMap As Map
  
    LengthsMap = GetMaxFieldLengths(ListOfMaps, ListHeader, "X", "")     'Get Max Field Lengths for each field in each row of the list of Skills, Spells, etc.
  
    'Header Row
    Dim cs As CSBuilder : cs.Initialize()
    ItemMap = ListOfMaps.Get(0)    'Get the header row.
    PaddedRow = GetPaddedCellContents(ItemMap, ListHeader, LengthsMap, JustificationMap, "X", "")    'Send that row to be padded using LengthsMap as guide for each column width.
  
    'Add each cell to the character string that will be shown via listview.
    For Each key In ListHeader
        cs.Append(CreateClickableWord(PaddedRow.Get(key)))
    Next
    cs.PopAll    'Close all open spans.  (What are spans?)
  
    'Display Listview
    ListHeaderLabel.Initialize("Header")    'Add a label as a header for the list.
    ListHeaderLabel.Padding = Array As Int (0dip, 0dip, 0dip, 0dip)    'Set the padding for that label.
    ListHeaderLabel.Typeface = Typeface.MONOSPACE
    ListHeaderLabel.TextSize = 15
    ListHeaderLabel.Text = cs
    cs.EnableClickEvents(ListHeaderLabel)

    'Set View Layout Parameters
    View.Initialize("View")
    View.Enabled = False
    View.SingleLineLayout.ItemHeight = 30dip
    Label1 = View.SingleLineLayout.Label
    Label1.TextSize = 15
    Label1.Typeface = Typeface.MONOSPACE
    Label1.Padding = Array As Int (0dip, 0dip, 0dip, 0dip)
  
    'Item Rows
    For i = 1 To ListOfMaps.Size -1
        Dim cs As CSBuilder : cs.Initialize()
        ItemMap = ListOfMaps.Get(i)    'Get one row (one skill, spell, etc with all modifiers shown).
        PaddedRow = GetPaddedCellContents(ItemMap, ListHeader, LengthsMap, JustificationMap, "X", "")    'Send that row to be padded using LengthsMap as guide for each column width.
        'Add each cell to the character string that will be shown via listview.
        For Each key In ListHeader
            cs.Append(PaddedRow.Get(key))
        Next
        cs.PopAll    'Close all open spans.  (What are spaqns?)
        View.AddSingleLine(cs)    'Add the character string (one padded row containing skill name and modifiers) to the view
    Next
  
    'Display Header and Listview.
    pnlTest.AddView(ListHeaderLabel,0,0,100%x, 50)    'Show the header label.
    pnlTest.AddView(View, 0, 60, 100%x, 100%y-63dip)    'Show the list.
End Sub

Sub GetPaddedCellContents(RowMap As Map, HeaderList As List, MaxLengthsMap As Map, Justification As Map, TrueAs As String, FalseAs As String) As Map
   'This sub pads the contents of each cell in ONE ROW based on the values from MaxLengthsMap so that each cell is the width required for the column to be the width calculated based on the width of the longest item in the column including the header witdth.
   Dim sf As StringFunctions : sf.Initialize()
   Dim CellContentsString As String
   Dim NumSpaces As Int
   Dim LeftSpaces As String
   Dim RightSpaces As String
   Dim ResultsMap As Map : ResultsMap.Initialize()
   
   'Build the row with padded values from the unpadded values given in RowMap and the justification of each value specified in the Justification Map.
   For Each ColumnName In HeaderList   'Walk the header list getting Column Names (Headers).  These are the keys to the RowMap passed to this sub.
     Dim cs As CSBuilder
     cs.Initialize()
     'Log("HeaderName = " & HeaderName)
     
     CellContentsString = RowMap.Get(ColumnName)   'Get the value of the item with the key ColumnName from the RowMap.
     'Log("Cell Contents = " & ContentsString)
     
     'We don't want to display the words true or false in the listview so we substitute whatever was specified in TrueAs and FalseAs.
     If CellContentsString = "true" Then
       CellContentsString = TrueAs
     End If
     If CellContentsString = "false" Then
       CellContentsString = FalseAs
     End If
     
     'Based on Justification of each cell specified in Justification Map, put spaces after, before or split on both sides of sell content.
     Select Justification.Get(ColumnName)   'Get the justification specified for the item with the key ColumnName from the Justification Map.
       Case "Right"
         cs.Append(sf.AddSpaces(MaxLengthsMap.Get(ColumnName) - sf.Len(CellContentsString)))
         cs.Append(CreateClickableWord(CellContentsString))
       Case "Left"
         cs.Append(CreateClickableWord(CellContentsString))
         cs.Append(sf.AddSpaces(MaxLengthsMap.Get(ColumnName) - sf.Len(CellContentsString)))
       Case "Center"
         NumSpaces = MaxLengthsMap.Get(ColumnName) - sf.Len(CellContentsString)
         LeftSpaces = sf.AddSpaces(Ceil(NumSpaces/2))   'For odd number of spaces, this will put the higher amount on the left.
         RightSpaces = sf.AddSpaces(Floor(NumSpaces/2))   'For odd number of spaces, this will put the lower amount on the right.
         cs.Append(LeftSpaces)
         cs.Append(CreateClickableWord(CellContentsString))
         cs.Append(RightSpaces)
     End Select
     cs.Append(" ")
     
     ResultsMap.Put(ColumnName, cs)   'Add the padded, justified value to the row.
   Next
   Return(ResultsMap)   'Return the padded, justified row.
End Sub

My header for the listview is a label assigned in line 31 of this code. It works.

My other listview data has no label that I can attach the cs.EnableClickEvents() to.

Does anyone know how to do this?

Thanks again for assistance!
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I suggest to use CustomListView. Here you can
 
Upvote 0
Top