B4J Question TableView - find row index

atiaust

Active Member
Licensed User
Longtime User
Hi All,

I have a TableView with the first column being a unique Int value.

I want search for the Int value in the first column and get the corresponding row index number then scroll the TableView to that position.

Any one know how to do it?

Thanks
 

Daestrum

Expert
Licensed User
Longtime User
I found the easiest way was to
a) Create a index list of the data in the column you want
b) Use indexof(??) on the list to get its position
c) Use scrollTo on the value from the list.
eg,
B4X:
Sub Process_Globals
 Private fx As JFX
 Private MainForm As Form
 Dim tv As TableView
 Dim index As List
 Dim tf As TextField
 Dim lab As Label
End Sub
Sub AppStart (Form1 As Form, Args() As String)
 MainForm = Form1
 MainForm.Show
 tv.Initialize("tv")
 tv.SetColumns(Array("one","two","three"))
 tv.setColumnWidth(0,100)
 tv.setColumnWidth(1,100)
 tv.setColumnWidth(2,100)
 MainForm.RootPane.AddNode(tv,0,0,300,300)
 For a = 0 To 100
 tv.Items.Add(Array(a,"dummy","dummy#"&a))
 Next
 tf.Initialize("tf") ' for the search item
 MainForm.RootPane.AddNode(tf,400,0,100,20)
 lab.Initialize("")
 MainForm.RootPane.AddNode(lab,400,50,100,20)
 buildIndexOn(2) ' build index on col 2
End Sub
Sub buildIndexOn(col As Int)
 index.Initialize
 For Each item() As Object In tv.Items
 index.Add(item(col)) 
 Next
End Sub
Sub tf_Action
 If index.IndexOf(tf.Text) <> -1 Then
 tv.ScrollTo(index.IndexOf(tf.Text))
 lab.Text = "Found"
 Else
 lab.Text = "Not found" 
 End If
End Sub
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Daestrum,

Thanks for your suggestion.

Your example works using buildIndexOn(2) but not when you change your code to buildIndexOn(0).

I have looked at the resulting index in debug and it looks correct but it always returns "Not Found"

My table has all int reference numbers in the first column ie 12034, 12899, 23456, 23457, 30120 etc.... this is what I need to search on.

Not sure what is going on.

Will keep working on it.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
It's probably caused by the object type being Integer for the first column.
maybe change
B4X:
For Each item() As Object In tv.Items
index.Add(item(col))
Next
to
B4X:
For Each item() As Object In tv.Items
dim myInt as integer = item(col)
index.Add(myInt)
Next
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thanks, had already tried something similar but it causes an error

java.lang.NumberFormatException: For input string: "Label@f62fb82[styleClass=label]'11'"

Not sure when the table gets created if the int in col(0) gets converted to a string.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Looks like column 0 contains a Label from the error message
maybe try
B4X:
For Each item() As Object In tv.Items
   Dim myLabel as Label = item(col)
   index.Add(myLabel.Text)
Next
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…