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

Status
Not open for further replies.
Edit: better to use B4XDialog + B4XSearchTemplate

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/
 

Attachments

  • SearchView.zip
    45.1 KB · Views: 2,368
Last edited:

WizardOz

Member
Licensed User
Longtime User
A really great and usefull Class! :sign0098:
 

barx

Well-Known Member
Licensed User
Longtime User
Looks very good.

Thanks
 

contoystudio

Member
Licensed User
Longtime 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.
 

susu

Well-Known Member
Licensed User
Longtime User
Thank you Erel. Is this class faster than original AutoCompleteEditText if I load 20,000 items?
 

Mahares

Expert
Licensed User
Longtime 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.

B4X:
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(Activity, 0, 10%x, 100%x, 90%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
 

Mahares

Expert
Licensed User
Longtime 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:
B4X:
Sub sv_ItemClick(Value As String)
   Value= Value.SubString2(0,Value.IndexOf(";"))     
    Msgbox("Selected value: " & Value, "")
End Sub
 

Mahares

Expert
Licensed User
Longtime 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:
B4X:
Public Sub ClearSearchBox    
    et.Text=""
    IME.ShowKeyboard(et)
End Sub

In the main module I have put the following:
B4X:
'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.
 

Mahares

Expert
Licensed User
Longtime 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.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code if from SearchView class:
B4X:
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
 

padvou

Active Member
Licensed User
Longtime 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


You can add it to a Panel and then add it to any layout you need.
 
Status
Not open for further replies.
Top