B4J Question TableView Delete Row based on Value in TableView

aaronk

Well-Known Member
Licensed User
Hi,

I am trying to delete a item from a TableView based on what is set in Colum 1.

I am using the following code and can't seem to work out how to delete the a row based on the value.

B4X:
Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    MainForm.Title = "Table Example"
    'set the columns
    tv1.SetColumns(Array As String("Col1", "Col2"))
    'fill with data
    'each row must be an array of objects. Make sure not to reuse the same array!
  
    Dim row(2) As Object
        row(0) = "Bob"
        'the second item will be later edited by the user. To allow it to be updated we use a label.
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Text = "1"
        row(1) = lbl
        tv1.Items.Add(row)
      
    Dim row(2) As Object
        row(0) = "john"
        'the second item will be later edited by the user. To allow it to be updated we use a label.
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Text = "2"
        row(1) = lbl
        tv1.Items.Add(row)
      
    Dim row(2) As Object
        row(0) = "bill"
        'the second item will be later edited by the user. To allow it to be updated we use a label.
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Text = "3"
        row(1) = lbl
        tv1.Items.Add(row)
      
    Dim row(2) As Object
        row(0) = "Jack"
        'the second item will be later edited by the user. To allow it to be updated we use a label.
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Text = "4"
        row(1) = lbl
        tv1.Items.Add(row)
      
  
    tv1.SetColumnSortable(1, False)
    btnUpdate.Enabled = False
End Sub
Lets say I want to Delete the row if column 1 = bill.

I have tried using the following code, but it doesn't seem to like..

B4X:
For i = 0 To tv1.Items.Size -1
        If tv1.SelectCell(i,1) = "bill" Then
            tv1.Items.RemoveAt(i)
        End If
    Next
B4X:
Error description: Object reference not set to an instance of an object.
Occurred on line: 64
If tv1.SelectCell(1,1) = "bill" Then
Word: )
I guess I am doing something wrong, does anyone know how to delete a row based on the value that is in column 1 ?
 

rwblinn

Well-Known Member
Licensed User
Hi,

try
B4X:
For i = 0 To tv1.Items.Size -1
  Dim row() As Object = tv1.Items.Get(i)
  Dim name As String = row(0)
  If name = "bill" Then
    tv1.Items.RemoveAt(i)
    Return
  End If
Next
 
Top