B4J Question [SOLVED][ABMaterial] ABMTable.AddRow(Unknow Source)

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
a bit of practice to learn more ABMaterial framework (thanks Harris for your tutorial).
I have a page "Home" with this code (a part of the code)
B4X:
public Sub ConnectPage()          
    '    connecting the navigation bar
    ABMShared.ConnectNavigationBar(page)
    ' refresh the page
    page.Refresh
    ' Tell the browser we finished loading
    page.FinishedLoading
    ' restoring the navigation bar position
    page.RestoreNavigationBarPosition  
    page.NavigationBar.Refresh
End Sub

Sub Page_NavigationbarClicked(Action As String, Value As String)
    ' saving the navigation bar position
    page.SaveNavigationBarPosition
    If Action = "LogOff" Then
        ABMShared.LogOff(page)
        Return
    End If
    Select Action
        Case "Contact"
            ShowMessage("Licenza: " & ws.Session.GetAttribute("licenza"), "Info Utente-Licenza")
            Return
        Case "Clienti"
            SetTableClienti
    End Select
    'ABMShared.NavigateToPage(page.ws, ABMPageId, Value)'"../")
End Sub

Sub SetTableClienti
    page.Row(1).RemoveAllComponents
    page.Row(2).RemoveAllComponents
    'input search in DB
    Dim searchClient As ABMInput
    searchClient.Initialize(page, "CercaCliente", ABM.INPUT_TEXT, "Cerca Cliente", False, "lightblue")
    page.Cell(1,1).AddComponent(searchClient)
    '
    'Button search
    Dim btnSearch As ABMButton
    btnSearch.InitializeFloating(page, "btnSearch", "mdi-action-search", "")
    page.Cell(1,7).AddComponent(btnSearch)
    '
    'Pagination
    Dim pagination As ABMPagination
    pagination.Initialize(page, "pagination", 5, True, True, "")
    pagination.SetTotalNumberOfPages(0)
  
    page.Cell(2,1).AddComponent(pagination)
  
    ' create a ABMtable to show records
    Dim tblarc As ABMTable
    tblarc.Initialize(page, "tblarc", False, False, True, "tbltheme")
    tblarc.SetHeaders(Array As String("ID", "CODICE", "CLIENTE", "NOTE", "Carica/Edit", "Cancella"))
    tblarc.SetHeaderThemes(Array As String("bgc", "bgc", "bgc", "bgc", "bgc", "bgc"))
    tblarc.SetColumnVisible(Array As Boolean(False, True, True, True, True, True))
    tblarc.IsResponsive = True
    tblarc.IsBordered = True
    tblarc.SetFooter("Numeri record: 0", 12, "bg")
    ' NOTE: We are using the CellR method (relative to current row)...
    page.Cell(2,2).AddComponent(tblarc)
    'load from DB
    LoadClientiDB(1)
  
    page.Refresh  
End Sub

Sub LoadClientiDB(fromPage As Int)
    ' Get the ABMTable component...
    Dim tblarc As ABMTable = page.Component("tblarc")  ' get the table defined in SetTableClienti...
    Dim sql As SQL = DBM.GetSQL
    Dim numclienti As Int = DBM.SQLSelectSingleResult(sql, "SELECT Count(ID) as IDS FROM CLIENTI")
    Dim clienti As List = DBM.SQLSelect(sql, "SELECT * FROM CLIENTI LIMIT " & ((fromPage - 1) * iRecs) & ", " & iRecs, Null)
    If clienti.Size = 0 And fromPage > 1 Then
        DBM.CloseSQL(sql)
        fromPage = fromPage - 1
        LoadClientiDB(fromPage)
        Return
    End If
  
    If clienti.Size = 0 Then
        Dim searchUser As ABMInput = page.Component("searchUser")
        searchUser.Text = ""
        searchUser.Refresh
        page.ShowToast("srch1",""," Nessun Record trovato! Ricerca di nuovo...",2000,False)
    End If
  
    tblarc.SetFooter("Numeri record: " & numclienti, 12,"bgc")
  
    ' clear any old stuff in the table so we may add new...
    tblarc.Clear
    'add
    For i = 0 To clienti.Size - 1
        Dim tblfields As Map = clienti.Get(i)
        Dim rCellValues As List
        Dim rCellThemes As List
        rCellValues.Initialize
        rCellThemes.Initialize
        For v = 0 To tblfields.Size - 1
            rCellValues.Add(tblfields.GetValueAt(v))
            rCellThemes.Add("nocolor")
        Next
        'button edit
        Dim btnEdit As ABMButton
        btnEdit.InitializeFloating(page, "btnEdit", "mdi-action-visibility", "")
        rCellValues.Add(btnEdit)
        rCellThemes.Add("openedit")
        'button cancella
        Dim btnDelete As ABMButton
        btnDelete.InitializeFloating(page, "btnDelete", "mdi-action-delete",  "btnred")
        btnDelete.Size = ABM.BUTTONSIZE_SMALL
        rCellValues.Add( btnDelete)
        rCellThemes.Add( "openedit")
        tblarc.AddRow("uid_" & i, rCellValues)  '<<<<<<<<< Error
        tblarc.SetRowThemes(rCellThemes)     '<<<<<<<<< Error
    Next
    tblarc.Refresh
    DBM.CloseSQL(sql)
    'pagination
    Dim pagination As ABMPagination = page.Component("pagination")
  
    If (numclienti Mod iRecs > 0) Or (numclienti = 0) Then
        numclienti = numclienti/iRecs + 1
    Else
        numclienti = numclienti/iRecs
    End If
    pagination.SetTotalNumberOfPages(numclienti)
    pagination.SetActivePage(fromPage)
  
    pagination.Refresh
End Sub

here appears the error:
B4X:
tblarc.AddRow("uid_" & i, rCellValues)  '<<<<<<<<< Error
tblarc.SetRowThemes(rCellThemes)     '<<<<<<<<< Error

This the error:
at com.ab.abmaterial.ABMTable.AddRow(Unknown Source)

If i comment this lines the Table is displayed.

Thanks for your help.

 

Harris

Expert
Licensed User
Longtime User
If i comment this lines the Table is displayed.

Commented out, does the table show any data - with edit and delete buttons?

How many fields are in " CLIENTI " database table?
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Commented out, does the table show any data - with edit and delete buttons?
Is show only table header and table footer because rCellValues is not loaded
I think that (Unknown Source) is reported to rCellValues and rCellThemes (but I do not understand why)

How many fields are in " CLIENTI " database table?
4: "ID","CODICE","CLIENTE","NOTE"

In table header i have: "ID","CODICE","CLIENTE","NOTE","Carica/Edit","Cancella"

Thanks
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Let's log some points of interest - cause it looks good to me... at this point.
Don't worry - you will get it - when you discover what is causing the error.

B4X:
  For v = 0 To tblfields.Size - 1

            rCellValues.Add(tblfields.GetValueAt(v))
            rCellThemes.Add("nocolor")

Log( " What is this field value: "&tblfields.GetValueAt(v) )

        Next

Run in debug and throw in many Log(" at this point ") - to see where it all goes sideways...

"Dim clienti As List = DBM.SQLSelect(sql, "SELECT * FROM CLIENTI LIMIT " & ((fromPage - 1) * iRecs) & ", " & iRecs, Null)"
- Selecting ALL ( * ) can bite you later...

Include your BuildPage() method as well so I can see how you structured the page grid.
In ABM, many things MUST be created in the proper place - else we get strange errors.

Study the example below (long and drawn out), you can see my query requests the exact fields of the table.
This is the preferred method since YOU know what you are dealing with...

I also take precaution to ensure field values are NOT null - cause this will blow up (doesn't like "Null" as a value)...

This is typical of how to build the ABMtable - Format cells as Large Bold labels, put in a checkbox, and an image, etc)

Pay attention to: ' NEVER format the primary key (as a label)!!!! It is generally hidden and needed to get which row was selected.
' A label format will not return the value of this cell...

B4X:
private Sub Loadreport(fromPage As Int)

    ' Here is where we populate the ABMtable with records from our MySQL table
    ' Since we don't want all the records initially (could be hundreds), we will use pagination to limit what is fetched from the server
    ' Use the query to order your table if you desire

    ' Get the ABMTable component...
    Dim tblUsers As ABMTable = page.Component("tblUsers")  ' get the table defined in ConnetPage...
   
    ' create a SQL cursor
    Dim SQL As SQL = DBM.GetSQL
   
    ' get number of users (select count query) - used in table footer and for pagination....
    Dim numusers As Int = DBM.SQLSelectSingleResult(SQL, "SELECT Count(pk) as IDS FROM reports ")
   
    '**********************************************************************************************************
    '**********************************************************************************************************
    ' Get a list of records - LIMIT these by pagination (page number) and number of records to show - iRecs (global var set to 4 currently)
    ' NOTE: We are selecting "certain" fields from the table - and in a certain order - the same order as title headers
    ' tblUsers.SetHeaders( Array As String ("ID"  , "Access Type"   , "Person's Name"    , "Login (username)"    , "Password"    ,"Photo", "Active" ,"Open/Edit", "Delete"  ))
    '**********************************************************************************************************
    '**********************************************************************************************************
    Dim qry As String =  "SELECT pk, repname, repfile,p1,p2,p3,p4,p5,rtype FROM reports " & " LIMIT " & ((fromPage - 1) * iRecs) & ", "&iRecs
    Dim users As List = DBM.SQLSelect(SQL, qry)
    If users.Size = 0 And fromPage > 1 Then
        ' we are on a page without any lines (maybe removed by other user?)
        DBM.CloseSQL(SQL)
        fromPage = fromPage - 1
        Loadreport(fromPage)
        Return
    End If
   
   
    tblUsers.SetFooter("Total number of reports: " & numusers, 12,"bgc")
   
    ' clear any old stuff in the table so we may add new...
    tblUsers.Clear
   
   
    For i = 0 To users.Size - 1
   
        ' our list is a map of field names  and values
        ' we create a new map for each list item
        Dim tblFields As Map = users.Get(i)
        '
        Dim rCellValues As List
        Dim rCellThemes As List
        rCellValues.Initialize
        rCellThemes.Initialize

        '**********************************************************************************************************
        '**********************************************************************************************************
        ' In this example, we shall add ABMTable fields by users.field name...
        ' It does not matter in what order we add items now since our query defined the order based on our ABMTable headers...
        '**********************************************************************************************************
        '**********************************************************************************************************

        For j = 0 To tblFields.Size - 1  ' set the var - Value of map...
           
            ' This "var" is used for each "GetKeyAt" that follows....
            Dim var As String
            var =     tblFields.GetValueAt(j)
            If var = "null" Then             ' init as string to avoid null_pointer error
                var = ""
            End If

'  NEVER format the primary key (as a label)!!!!   It is generally hidden and needed to get which row was selected.
' A label format will not return the value of this cell...
            If tblFields.GetKeyAt(j) = "pk" Then 
                rCellValues.Add(var)
                rCellThemes.Add("nocolor")
                Continue
            End If
'  NEVER format the primary key (as a label)!!!!   It is generally hidden and needed to get which row was selected.


           
            If tblFields.GetKeyAt(j) = "repname" Then
                Dim pd As ABMLabel
                pd.Initialize(page,"usrn"&j, var ,ABM.SIZE_H5,False,"")
                rCellValues.Add(pd)
                rCellThemes.Add("nocolor")
                Continue
            End If

            If tblFields.GetKeyAt(j) = "repfile" Then
                Dim pd As ABMLabel
                pd.Initialize(page,"usrl"&j,var,ABM.SIZE_PARAGRAPH,False,"")
                rCellValues.Add(pd)
                rCellThemes.Add("nocolor")
                Continue
            End If

            If tblFields.GetKeyAt(j) = "p1" Then
                Dim cb As ABMCheckbox
                Dim nvar As Int        ' convert var to int
                nvar = var
                cb.Initialize(page, "ucb"&j,  "", (nvar > 0) ,"")   ' set checkbox state (True / False) with nvar logic
                cb.Enabled = False

                rCellValues.Add(cb)
                rCellThemes.Add("nocolor")
                Continue
            End If

            If tblFields.GetKeyAt(j) = "p2" Then
                Dim cb As ABMCheckbox
                Dim nvar As Int
                nvar = var
                cb.Initialize(page, "ucb"&j,  "", (nvar > 0) ,"")
                cb.Enabled = False

                rCellValues.Add(cb)
                rCellThemes.Add("nocolor")
                Continue
            End If
            If tblFields.GetKeyAt(j) = "p3" Then
                Dim cb As ABMCheckbox
                Dim nvar As Int
                nvar = var
                cb.Initialize(page, "ucb"&j,  "", (nvar > 0) ,"")
                cb.Enabled = False

                rCellValues.Add(cb)
                rCellThemes.Add("nocolor")
                Continue
            End If

            If tblFields.GetKeyAt(j) = "p4" Then
                Dim cb As ABMCheckbox
                Dim nvar As Int
                nvar = var
                cb.Initialize(page, "ucb"&j,  "", (nvar > 0) ,"")
                cb.Enabled = False

                rCellValues.Add(cb)
                rCellThemes.Add("nocolor")
                Continue
            End If

            If tblFields.GetKeyAt(j) = "p5" Then
                Dim cb As ABMCheckbox
                Dim nvar As Int
                nvar = var
                cb.Initialize(page, "ucb"&j,  "", (nvar > 0) ,"")
                cb.Enabled = False
                rCellValues.Add(cb)
                rCellThemes.Add("nocolor")
                Continue
            End If
           
            If tblFields.GetKeyAt(j) = "rtype" Then
                Dim reptype As String = "Config"
                If var > 0 Then
                    reptype = "Application"
                End If
                Dim pd As ABMLabel
                pd.Initialize(page,"usrl"&j,reptype,ABM.SIZE_PARAGRAPH,False,"")
                rCellValues.Add(pd)
                rCellThemes.Add("nocolor")
                Continue
            End If

        Next
       
        ' add our edit and delete buttons to each row of the table
        ' the edit button is blue (by default - no theme)
       
        Dim btnEdit As ABMButton
        btnEdit.InitializeFloating(page, "btnEdit" ,"mdi-action-visibility", "")
       
        ' TIP: uncomment the line below and watch the height of each row shrink.  Use small buttons for more rows per page when required
        ' btnEdit.Size = ABM.BUTTONSIZE_SMALL
       
        rCellValues.Add(btnEdit)
        rCellThemes.Add("nocolor")

        ' Now add a ABMTable row with the list items.  This list must match the number of headers you defined for this abmtable!
        tblUsers.AddRow("uid_"&i,  rCellValues)

        ' make sure you have as many items in rCellThemes as in rCellValues!  Must follow IMMEDIATELY AFTER AddRow!
        tblUsers.SetRowThemes( rCellThemes)
    Next

    ' refresh the table!!!
    tblUsers.Refresh
   
    DBM.CloseSQL(SQL)
   
   
    ' Here we are setting the pagination component.
    ' It determines how many pages we have - and which page is currently in view.
    Dim pagination As ABMPagination = page.Component("pagination")
   
    If (numusers Mod iRecs > 0) Or (numusers = 0) Then
        numusers = numusers/iRecs + 1
    Else
        numusers = numusers/iRecs
    End If

   
    pagination.SetTotalNumberOfPages(numusers)
    pagination.SetActivePage(fromPage)
    pagination.Refresh
   
    Log(" Finished loading reports... ")
   
   
End Sub
 
Last edited:
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
(doesn't like "Null" as a value).
Infact.
Null value in "NOTE" fields

B4X:
For v = 0 To tblfields.Size - 1
   s = tblfields.GetValueAt(v)
   If s.EqualsIgnoreCase("null") Then
         rCellValues.Add("")
   Else
         rCellValues.Add(s)
   End If
   rCellThemes.Add("nocolor")
Next

Now Work.

Thanks for your availability
 
Upvote 0
Top