B4J Question [B4XTable] Implementing A New Search Function

cklester

Well-Known Member
Licensed User
I've got a searching algorithm I'm using to filter a CustomListView.

I can enter a few space-delimited search terms into the search text box; e.g., using "android iphone" would not only match "android iphone" but also "iphone android".

It even works for partial "words"; e.g., "andro ipho" would basically match the same way as above.

I would love to be able to use it for a B4XTable! Anybody know how to do that?

B4X:
'Does string txt contain all words in list s?
Private Sub ContainsAll(txt As String, s As List) As Boolean
    Private does As Boolean = True
    For Each item As String In s
        does = does And txt.tolowercase.Contains(item.ToLowerCase)
    Next
    Return does
End Sub

Private Sub txt_Search_TextChanged (Old As String, New As String)
    clv_Exercises.Clear
    clvExercisesSelections.SelectedItems.Clear

    If New.Length = 0 Then
        For Each item As String In AllExercises
            clv_Exercises.AddTextItem(item,item)
        Next
    Else
        Private terms As List = Regex.Split(" ",New.Trim)
        Private xlist As List
        xlist.Initialize
        
        For Each item As String In AllExercises
            If ContainsAll(item,terms) Then
                clv_Exercises.AddTextItem(item,item)
            End If
        Next
    End If
End Sub

(Next step would make this a fuzzy search, where best matches are listed first and partial matches are included further down the list (or sorted by Levenshtein distance).)
 
Top