B4A Library [Class] SearchView - More powerful alternative to AutoCompleteEditText

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jul 8, 2012.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    SearchView is made of an EditText and ListView. When the user enters text into the EditText the ListView shows the items that start with this text or that contain the text (in this order).

    This view is useful for allowing the user to select an item from many items.

    Advantages over AutoCompleteEditText:
    • SearchView uses an internal index that is built when you call SetItems. This allows it to quickly find the matches.
    • SearchView also shows items that contain the input text (not just prefixes).
    • The class code can be further customized as needed.

    upload_2017-2-21_17-48-19.png

    Tutorial about handling large, searchable lists: https://www.b4x.com/android/forum/t...e-list-with-searchview-b4xserializator.61872/
     

    Attached Files:

    Last edited: May 19, 2019
  2. WizardOz

    WizardOz Member Licensed User

    A really great and usefull Class! :sign0098:
     
  3. JorgeMC

    JorgeMC Member Licensed User

    Thanks !!!!
     
  4. barx

    barx Well-Known Member Licensed User

    Looks very good.

    Thanks
     
  5. spillo3d96

    spillo3d96 Member Licensed User

    It looks nice!

    Thanks!
     
  6. dealsmonkey

    dealsmonkey Active Member Licensed User

    Thank you Erel, really useful class.
     
  7. Jost aus Soest

    Jost aus Soest Active Member Licensed User

    Great!
    :sign0098:
     
  8. contoystudio

    contoystudio Member Licensed User

    Great work. That' s what i'm looking for!!:sign0098:
    I use "AHViewPager", there' a way to use that search view into a layout?:sign0104:
    Thanks.
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    You can add it to a Panel and then add it to any layout you need.
     
  10. susu

    susu Well-Known Member Licensed User

    Thank you Erel. Is this class faster than original AutoCompleteEditText if I load 20,000 items?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    AutoCompleteEditText doesn't use an index. Once the index is built (after you call SetItems), this implementation is much faster. AutoCompleteEditText only checks for prefixes while SearchView works with any substring as well.
     
  12. Mahares

    Mahares Well Known Member Licensed User

    I use the SearchView CLASS module to help me search inside a table. I would like to search on the first of 2 columns diplayed in the list. The 2 column contents are separated by a semi colon or any separator for that matter. The second column will be in the list, but not used for search. Unfortunately, the search is done across both columns. Thank you for any tips.

    Code:
    Dim SQL1 As SQL           'in process global
    Dim index As Object       'in process global

    Dim sv As SearchView   ' in globals
    Dim Cursor1 as Cursor   ' in globals
    Dim MyList as List          ' in globals
    Dim txt, DBFilePath, DBFileName, MyFolder as String            ' in globals
    sv.Initialize(Me, "sv")
       sv.AddToParent(
    Activity010%x100%x90%y)   

       
    If File.ExternalWritable Then 
          DBFilePath = 
    File.DirRootExternal & "/" & MyFolder
       
    Else 
          DBFilePath = 
    File.DirDefaultExternal & "/" & MyFolder
       
    End If
       
    File.MakeDir(DBFilePath,MyFolder)
       
    If FirstTime Then 
          MyList.Initialize
          SQL1.Initialize(DBFilePath, DBFileName, 
    True)
          txt=
    "SELECT F1, F2 FROM MyTable ORDER BY F1"
          Cursor1=SQL1.ExecQuery(txt)
          Cursor1.Position=
    0
          
    For i=0 To Cursor1.RowCount-1
            Cursor1.Position = i
             MyList.Add(Cursor1.GetString(
    "F1") & ";" & Cursor1.GetString("F2"))
          
    Next
          index = sv.SetItems(MyList)
       
    Else
          sv.SetIndex(index)
       
    End If
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    It cannot be done without modifying the class code. You will need to see how the index step works and modify it to only index the first part.
     
  14. Mahares

    Mahares Well Known Member Licensed User

    I could not figure out the ins and outs of the sub in the CLASS module to modify it: Public Sub SetItems(Items As List) As Object. It is too intricate.
    Therefore, I just accepted that it cannot be done and did my search across both listed fields in the list. In the selected item sub, I built the following sub to extract only the first column data:
    Code:
    Sub sv_ItemClick(Value As String)
       Value= Value.SubString2(
    0,Value.IndexOf(";"))     
        
    Msgbox("Selected value: " & Value, "")
    End Sub
     
  15. Mahares

    Mahares Well Known Member Licensed User

    @Erel: I have added a little bit of code to the CLASS module to clear the search box and prepare for a new search:
    Code:
    Public Sub ClearSearchBox    
        et.Text=
    ""
        
    IME.ShowKeyboard(et)
    End Sub
    In the main module I have put the following:
    Code:
    'To clear the search box
    Sub btnClear_Click  
       sv.ClearSearchBox
    End Sub
    My question is: How do you capture the search string value of the item selected immeditely before I trigger the sv_ItemClick event?
    Thank you a very useful and practical library.
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    Not sure that I understand the question.

    You should check lv_ItemClick event. The clicked item is set as the EditText text. You can take the original value before setting it.
     
  17. Mahares

    Mahares Well Known Member Licensed User

    My question is rather simple, but the answer escapes me:
    If I insert in the search box: 'ze' without the quotes, the list will show: New Zealand, Switzerland, etc.
    I would like to know how I capture the word 'ze' I typed in the search string as a variable immediately before the item click event.
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    This code if from SearchView class:
    Code:
    Private Sub lv_ItemClick (Position As Int, Value As Object)
    'add this line:
    Msgbox("Current value: " & et.Text, "")   
    et.Text = Value
       et.SelectionStart = et.Text.Length
       
    Dim IME As IME
       
    IME.HideKeyboard
       lv.Visible = 
    False
       
    If SubExists(mCallback, mEventName & "_ItemClick"Then
          
    CallSub2(mCallback, mEventName & "_ItemClick", Value)
       
    End If
    End Sub
     
  19. padvou

    padvou Active Member Licensed User

    Question

    I'm new here. How exactly do you add it to a Panel? I tried something but i get Layout not available. Could you please post some code snippet?
    Thank you


     
  20. Erel

    Erel Administrator Staff Member Licensed User

    You should first add a panel to the activity (by code or with the designer) and then pass the panel as the first parameter instead of the activity.
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice