B4J Question checkbox in Tableview

Peter Lewis

Active Member
Licensed User
Longtime User
Hi All

I have tried all the posts with regards to this and none of them work, unless I missed one.

I want to have checkboxes per line or select all.

Does anyone have any ideas on this ?

Thank you
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
B4X:
Dim r(3) as object 
r(0) = "simple text" 
r(1) = "another simple text" 

Dim cb as checbox 
cb.initialize("twcb") 
cb.checked = true 

r(2) = cb 

Tw.items.add(r)

To select all:

B4X:
For each r() as object in Tw.items
Dim cb as checkbox = r(2) 
cb.checked = true 
Next
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
B4X:
Dim r(3) as object
r(0) = "simple text"
r(1) = "another simple text"

Dim cb as checbox
cb.initialize("twcb")
cb.checked = true

r(2) = cb

Tw.items.add(r)

To select all:

B4X:
For each r() as object in Tw.items
Dim cb as checkbox = r(2)
cb.checked = true
Next

That works well. it looked similar to the other one I found but it did not work.

If you load the tableview with one of the dbutils subs, can this be added afterwards or will I have to load the tableview manually and then include this as one of the items ?
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
you mean ExecuteTableView.

Yes, it can be added, afterwards,

B4X:
'Extract the row from the tableview:
dim r as object = tw.rows.get(i)

'add the node 
Dim cb as checbox 
cb.initialize("twcb") 
dim bitValue as int = r(2)
cb.checked = bitValue = 0
r(2) = cb
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
you mean ExecuteTableView.

Yes, it can be added, afterwards,

B4X:
'Extract the row from the tableview:
dim r as object = tw.rows.get(i)

'add the node
Dim cb as checbox
cb.initialize("twcb")
dim bitValue as int = r(2)
cb.checked = bitValue = 0
r(2) = cb
Would it not be better to do it in DBUTILS and add another sub just for this function so that the Checkbox is at the beginning of each row and the rest of the code in DBUTILS.ExecuteTableView will run like normal ?

I just think that would be loading tableview twice , once with the origional info and then read the table , add the Checkbox and load the table with the checkbox again.

Or am I not understanding it correctly ?
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
exactly! ExecuteTableView3 would be the right name for it!

For now it only has checkbox, but as you can see it may be easy to add the other Nodes.
B4X:
dim m as map
m.initialize

m.put(4,"checkbox") ' columnIndex and node to add

'if the column4 is and integer it will change the checkbox check state, if not it will be its text.
Public Sub ExecuteTableView3(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, _
    TableView1 As TableView,instructions As Map)
    TableView1.Items.Clear
    Dim cur As ResultSet
    If StringArgs = Null Then
        Dim StringArgs(0) As String
    End If
    cur = SQL.ExecQuery2(Query, StringArgs)
    Dim cols As List
    cols.Initialize
    For i = 0 To cur.ColumnCount - 1
        cols.Add(cur.GetColumnName(i))
    Next
    TableView1.SetColumns(cols)
    Do While cur.NextRow
        Dim values(cur.ColumnCount) As Object
        For col = 0 To cur.ColumnCount - 1
            If instructions.ContainsKey(col) Then
                Dim instruction As String = instructions.Get(col)
              
                Select instruction.ToLowerCase
                    Case "checkbox"
                        Dim cb As CheckBox
                        cb.Initialize("twcb")
                      
                        If IsNumber(cur.GetString(col)) Then
                            cb.Checked = cur.GetInt(col) = 0
                        Else
                            cb.Text = cur.GetString(col)
                        End If
                        values(col) = cb
                End Select
            Else
                values(col) = cur.GetString2(col)
            End If
        Next
        TableView1.Items.Add(values)
        If Limit > 0 And TableView1.Items.Size >= Limit Then Exit
    Loop
    cur.Close
End Sub
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
exactly! ExecuteTableView3 would be the right name for it!

For now it only has checkbox, but as you can see it may be easy to add the other Nodes.
B4X:
dim m as map
m.initialize

m.put(4,"checkbox") ' columnIndex and node to add

'if the column4 is and integer it will change the checkbox check state, if not it will be its text.
Public Sub ExecuteTableView3(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, _
    TableView1 As TableView,instructions As Map)
    TableView1.Items.Clear
    Dim cur As ResultSet
    If StringArgs = Null Then
        Dim StringArgs(0) As String
    End If
    cur = SQL.ExecQuery2(Query, StringArgs)
    Dim cols As List
    cols.Initialize
    For i = 0 To cur.ColumnCount - 1
        cols.Add(cur.GetColumnName(i))
    Next
    TableView1.SetColumns(cols)
    Do While cur.NextRow
        Dim values(cur.ColumnCount) As Object
        For col = 0 To cur.ColumnCount - 1
            If instructions.ContainsKey(col) Then
                Dim instruction As String = instructions.Get(col)
             
                Select instruction.ToLowerCase
                    Case "checkbox"
                        Dim cb As CheckBox
                        cb.Initialize("twcb")
                     
                        If IsNumber(cur.GetString(col)) Then
                            cb.Checked = cur.GetInt(col) = 0
                        Else
                            cb.Text = cur.GetString(col)
                        End If
                        values(col) = cb
                End Select
            Else
                values(col) = cur.GetString2(col)
            End If
        Next
        TableView1.Items.Add(values)
        If Limit > 0 And TableView1.Items.Size >= Limit Then Exit
    Loop
    cur.Close
End Sub

I tried the code and it did not work so I added a column in the SQLITE table and also did not help. I tried 0 as the position in the table I wanted the Checkbox. It was not happy with 0 or 1 but 10 the program ran but no display

B4X:
    Dim m As Map
    m.initialize

    m.put(10,"checkbox") ' columnIndex and node to add

    DBUTILS.ExecuteTableView3(DB.sql1, "Select checkbox, Barc, sku, lab,carat,colour,clarity,shape,discount, ApproNo FROM Appro_footer WHERE ApproNo = ? ", Array As String (appno),1000,TblInvFoot,m)
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Sorry my bad,

forgot to add the 2 in the resulset, this is the correct one (tested)

B4X:
'i used this query to test, it is the one from DBUtils demo from erel
    Dim m As Map 
    m.Initialize
   
    m.Put(4,"checkbox")
   
    DBUtils.ExecuteTableView3(SQL1, "SELECT Id, [First Name], [Last Name], Birthday, 0 checkbox FROM Students", Null, 0, TableView1,m)

Public Sub ExecuteTableView3(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, _
    TableView1 As TableView,instructions As Map)
    TableView1.Items.Clear
    Dim cur As ResultSet
    If StringArgs = Null Then
        Dim StringArgs(0) As String
    End If
    cur = SQL.ExecQuery2(Query, StringArgs)
    Dim cols As List
    cols.Initialize
    For i = 0 To cur.ColumnCount - 1
        cols.Add(cur.GetColumnName(i))
    Next
    TableView1.SetColumns(cols)
    Do While cur.NextRow
        Dim values(cur.ColumnCount) As Object
        For col = 0 To cur.ColumnCount - 1
            If instructions.ContainsKey(col) Then
                Dim instruction As String = instructions.Get(col)
              
                Select instruction.ToLowerCase
                    Case "checkbox"
                        Dim cb As CheckBox
                        cb.Initialize("twcb")
                      
                        If IsNumber(cur.GetString2(col)) Then
                            cb.Checked = cur.GetInt2(col) <> 0
                        Else
                            cb.Text = cur.GetString2(col)
                        End If
                        values(col) = cb
                End Select
            Else
                values(col) = cur.GetString2(col)
            End If
        Next
        TableView1.Items.Add(values)
        If Limit > 0 And TableView1.Items.Size >= Limit Then Exit
    Loop
    cur.Close
End Sub
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Sorry my bad,

forgot to add the 2 in the resulset, this is the correct one (tested)

B4X:
'i used this query to test, it is the one from DBUtils demo from erel
    Dim m As Map
    m.Initialize
 
    m.Put(4,"checkbox")
 
    DBUtils.ExecuteTableView3(SQL1, "SELECT Id, [First Name], [Last Name], Birthday, 0 checkbox FROM Students", Null, 0, TableView1,m)

Public Sub ExecuteTableView3(SQL As SQL, Query As String, StringArgs() As String, Limit As Int, _
    TableView1 As TableView,instructions As Map)
    TableView1.Items.Clear
    Dim cur As ResultSet
    If StringArgs = Null Then
        Dim StringArgs(0) As String
    End If
    cur = SQL.ExecQuery2(Query, StringArgs)
    Dim cols As List
    cols.Initialize
    For i = 0 To cur.ColumnCount - 1
        cols.Add(cur.GetColumnName(i))
    Next
    TableView1.SetColumns(cols)
    Do While cur.NextRow
        Dim values(cur.ColumnCount) As Object
        For col = 0 To cur.ColumnCount - 1
            If instructions.ContainsKey(col) Then
                Dim instruction As String = instructions.Get(col)
            
                Select instruction.ToLowerCase
                    Case "checkbox"
                        Dim cb As CheckBox
                        cb.Initialize("twcb")
                    
                        If IsNumber(cur.GetString2(col)) Then
                            cb.Checked = cur.GetInt2(col) <> 0
                        Else
                            cb.Text = cur.GetString2(col)
                        End If
                        values(col) = cb
                End Select
            Else
                values(col) = cur.GetString2(col)
            End If
        Next
        TableView1.Items.Add(values)
        If Limit > 0 And TableView1.Items.Size >= Limit Then Exit
    Loop
    cur.Close
End Sub

It almost worked, if I put the checkbox in column 1 then it adds the Checkbox to the date already in there, If I put it as column 0 then all the data from column goes away.

If it suppose to add to a column or make a new column ?

Whne I added the Column in the SQLITE file and used it at position no 1 along with 0 as a value, it worked.
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
It will depend on what the value on that column is:

if the column is a number, it will change the column data for the checkbox, it will then evaluate if the column is different from 0 if that is so then it will be checked, if not then it will be unchecked.

if the column is a date or as string or any other thing, it will add the value to text property of the checkbox.

i can delete that functionality if you want.
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
It will depend on what the value on that column is:

if the column is a number, it will change the column data for the checkbox, it will then evaluate if the column is different from 0 if that is so then it will be checked, if not then it will be unchecked.

if the column is a date or as string or any other thing, it will add the value to text property of the checkbox.

i can delete that functionality if you want.
All I have to do not is to determine which records on the screen are ticked so that I can process those records into an invoice. These are items that are on Appro.

I also made a print Pane which correctly formats the document along with all the terms and conditions. When the print button is pressed then it is visable and all other items are not until the print is finished.

appro.png
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
To filter those items that are not checked, based on that image:

B4X:
for i = tw.items.size -1 to 0 step -1
dim r() as object = tw.items.get(i)

dim cb as checkbox = r(0) 'column where the checkbox is

if not(cb.checked) then
tw.items.removeAt(i)
end if
next
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
To filter those items that are not checked, based on that image:

B4X:
for i = tw.items.size -1 to 0 step -1
dim r() as object = tw.items.get(i)

dim cb as checkbox = r(0) 'column where the checkbox is

if not(cb.checked) then
tw.items.removeAt(i)
end if
next

I wish it was that easy just to delete them. I just made a login screen so that we know who is logged into the program. Next I will make a transaction file that records everything everybody does and just dumps into a file so that I can rebuild everything if needed and if someone deletes I can see who it was and TIME DATE stamp it

Then when moving items out of Appro, the Appro number needs to be removed from the stock file and the avail flag needs to be reset. Then items leavoing appro going to Invoice needs to be updated and they make the sale but 2 fields needs to be updated before the final invoice price and these fields are per diamond. There can also be other diamonds from other Appro Notes to be included in the Invoice. The 2 fields are Exchange Rate and Discount amount. I was thinking of having a popup for these items using the same libruary I used for the Passwords ect jRLDialogs8
 
Upvote 0
Top