B4J Question I don't want events

PatrikCavina

Active Member
Licensed User
Longtime User
I'm working on tableview where when i click a row data of row are loaded in differents fields and when i want delete it I click DeleteButton that delete the selected row (very easy).
I implemented multiple selection of rows in TableView to delete more rows in one time only.
All selected rows are taken from GetSelectedItems function.

TableViewUtils
B4X:
Sub MultipleSelection(tv As TableView)
    Dim re As Reflector
    re.Target = tv
    re.Target = re.RunMethod("getSelectionModel")
    re.Target = re.RunMethod2("setSelectionMode","MULTIPLE","javafx.scene.control.SelectionMode")
End Sub

Sub GetSelectedItems(tv As TableView) As List
    Dim re As Reflector
    re.Target = tv
    re.Target = re.RunMethod("getSelectionModel")
    re.Target = re.RunMethod("getSelectedItems")
    Dim l As List
    l.Initialize
    l.AddAll(re.Target)
    Return l
End Sub

The problem rise when i delete all rows except one. The remained row is selected (grey selection like if focus changed) but don't rise TableView1_SelectedRowChanged(Index As Int, Row() As Object) so fields don't fill. All other deleted rows rise the event instead, that create more work for program.

Deleted rows function
B4X:
Private Sub deleteRecord_Click
    If TableView1.SelectedRow = -1 Then Return

    Dim rows As List = TableViewUtils.GetSelectedItems(TableView1)
    For i = 0 To rows.Size-1
        Dim row() As String = rows.Get(i)
        Dim code As String = row(0)
        Dim kCode As KdrCode = KConverter.ParseCode(code)
   
        ManageKCSV.KCSVClass.DeleteRecord(kCode)
   
        Dim index As Int = TableView1.Items.IndexOf(row)
        TableView1.Items.RemoveAt(index)
    Next
   
    MainKCSV.saved = False
End Sub

Assign fields value when change row
B4X:
Private Sub TableView1_SelectedRowChanged(Index As Int, Row() As Object)
    If TableView1.SelectedRow = -1 Or Index = -1 Then Return 'index = -1 se la selezione è multipla
   
    Dim code As String = Row(0)
    Dim kCode As KdrCode = KdrillsConverter.ParseCode(code)
    Dim kDescription As KdrDescription = ManageKCSV.KCSVClass.GetKdrDescription(kCode)
   
    'Set Layout
    diametroRec.Text = kDescription.Diametro
    lunghezzaRec.Text = kDescription.Lunghezza
    prezzoRec.Text = kDescription.Prezzo
    legantiRec.Items.Clear
    If kDescription.Legante.IsInitialized Then
        legantiRec.Items.AddAll(kDescription.Legante)
    End If
   
    ChangeSavedCheckStatus(True)
End Sub

So i would to understand how to lock TableView1_SelectedRowChanged events rised from deleted rows.
 

Cableguy

Expert
Licensed User
Longtime User
The simplest way if if to count the number of rows selected and compare against the total amount, and then only act if the difference is higher than one...
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
From what you explain, you're having issues updating the table when all rows except one are deleted...
Then all you need to do is, on selectedchanged, count the number of selected rows, and subtract it to the total amount of rows existing. If the result is more than 0. This way if your table is left with 1 row you just call selectionchanged again for it to populate the row correctly

[This has been edited and should make more sense now]
 
Upvote 0

PatrikCavina

Active Member
Licensed User
Longtime User
Ok now i think to understand what you mean.
B4X:
Private Sub deleteRecord_Click
    If TableView1.SelectedRow = -1 Then Return
 
    ClearFields
 
    Dim selectedRows As List = TableViewUtils.GetSelectedItems(TableView1)
 
    Dim selectionSize As Int = selectedRows.Size '<-----------Count selection size
    Dim tableRows As Int = TableView1.Items.Size '<---------Count items before to delete all
 
    For i = 0 To selectedRows.Size-1
        Dim row() As String = selectedRows.Get(i)
        Dim code As String = row(0)
        Dim kCode As KdrillsCode = KdrillsConverter.ParseCode(code)
 
        ManageKCSV.KCSVClass.DeleteRecord(kCode)
 
        Dim index As Int = TableView1.Items.IndexOf(row)
        TableView1.Items.RemoveAt(index)
    Next
 
    If tableRows - selectionSize = 1 Then
        CallSubDelayed3(Me,"TableView1_SelectedRowChanged",1,TableView1.Items.Get(0)) '<------- I put the event in queue, so it will execute like last event
    End If
 
    MainKCSV.saved = False
End Sub

Tell me if i fault because this solution works.
Although I would have preferred that nothing should be selected, but I don't understand why if I use
B4X:
TableView.SelectedRow = -1
does not deselect
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could use
B4X:
TableView.ClearSelection

To remove the highlighted item
 
Upvote 0
Top