B4i Library [class] SearchView

This class is similar to B4A SearchView class. It allows the user to filter a list of items based on a term (prefix matches first followed by other matches).

It uses the new RichString class to highlight the matching terms. Note that the scrolling and filtering performance in release mode are very good. It can be a bit jumpy in debug mode.


New version of SearchView is now available. It is implemented as a custom view. It also properly handles the keyboard state.
 

Attachments

  • SearchView.zip
    7.5 KB · Views: 333
Last edited:

John Woodsmall

Active Member
Licensed User
Longtime User
"New" from:"narek-adonts"
sv_ItemClick(Value As String) not returning a value when
B4X:
Dim states As List = File.ReadList(File.DirAssets, Temp_TextField.text & ".TXT")
        sv.SetItems(states)

any ideas on this?
 

Haris Hafeez

Active Member
Licensed User
Longtime User
"New" from:"narek-adonts"
sv_ItemClick(Value As String) not returning a value when
B4X:
Dim states As List = File.ReadList(File.DirAssets, Temp_TextField.text & ".TXT")
        sv.SetItems(states)

any ideas on this?
Just looked at this class. While I haven't spent much time on going through the actual code, I can see that click values are not being returned. This is because a TextField called 'et' is used (I'm not sure why). If you just want to have the value that is clicked upon returned to you in the callback, just replace et.text with cell.text.ToString in the following two methods:
tblSearchResult_SelectedChanged and lv_SelectedChanged

To the original author, please let us know if this is going to break something. So far it looks fine but I've only just ran the example.
 

Haris Hafeez

Active Member
Licensed User
Longtime User
SearchView raises the ItemClick event. You don't need to modify the code.
The question is about the value being passed to this callback. In my test and the one reported by John, the valued passed back is blank for which I had to modify the code.
 

tufanv

Expert
Licensed User
Longtime User
I have updated Erel's SearchView with using the iOS native SearchViewController (like in iMessage, Whatsapp, Viber,...)

It is just the basic wrapping so you can contribute to it.

For current users of SearchView you do not need modify anything in your code.

Project attached.

Narek

Dear Narek,

In this implmentation is it possible to change th listview is visible only after something is searched ? Altough the lv.visible is false the list of results are always visible without writing anything to search box ?
Also another question : ıs it possile to change the hinttext of searchbox. It is "search" at the moment and i want to change it to another language .
TY
 

narek adonts

Well-Known Member
Licensed User
Longtime User
In the "New" from:"narek-adonts" search view above (which I have added to my app) it does not seem to return a value to
the sv_ItemClick(Value As String)
.
On the first one (above) it works correctly. But on the new one which looks great(i would love to use it)
it does not seem to return a value. All i did was replace the new *.bas to my app.
Will post a fixed version today
 

narek adonts

Well-Known Member
Licensed User
Longtime User
Dear Narek,

In this implmentation is it possible to change th listview is visible only after something is searched ? Altough the lv.visible is false the list of results are always visible without writing anything to search box ?
Also another question : ıs it possile to change the hinttext of searchbox. It is "search" at the moment and i want to change it to another language .
TY
Didnt understood the first question
 

tufanv

Expert
Licensed User
Longtime User
Didnt understood the first question

What i mean is , there will be only a searchbox (the textfield ) and with some words written , the results will be seen. ın this example the results are visible from the begining .
 

narek adonts

Well-Known Member
Licensed User
Longtime User
What i mean is , there will be only a searchbox (the textfield ) and with some words written , the results will be seen. ın this example the results are visible from the begining .

It is a regular page. You can do whatever you want. hide, show, add views,...
 

tufanv

Expert
Licensed User
Longtime User
It is a regular page. You can do whatever you want. hide, show, add views,...
In the code module i tried lv.visible=false to make only et visible not the results but that hided also the textview.
The list of cities is visible before typing anything to searchbox. Can you tell how can i hide the results when noting is typed ,
 

narek adonts

Well-Known Member
Licensed User
Longtime User
In the code module i tried lv.visible=false to make only et visible not the results but that hided also the textview.
The list of cities is visible before typing anything to searchbox. Can you tell how can i hide the results when noting is typed ,
try lv.clear
 

narek adonts

Well-Known Member
Licensed User
Longtime User
In the code module i tried lv.visible=false to make only et visible not the results but that hided also the textview.
The list of cities is visible before typing anything to searchbox. Can you tell how can i hide the results when noting is typed ,


There are 2 options.

1. Add the search Textview to the page root panel and not to the tableview
2. Add a view over the table view
 

narek adonts

Well-Known Member
Licensed User
Longtime User
"New" from:"narek-adonts"
sv_ItemClick(Value As String) not returning a value when
B4X:
Dim states As List = File.ReadList(File.DirAssets, Temp_TextField.text & ".TXT")
        sv.SetItems(states)

any ideas on this?
Please see attached the fixed version.

It fixes the bug with Click Event and also and change the return type to TableCell instead of String.
 

Attachments

  • searchView.zip
    4.4 KB · Views: 60

Alvsky

Member
Licensed User
Longtime User
Hi all
Is there a way to change the background color of listed items.
I tried to set lv.Color in SearchView to some color but list background still remained white?!?
I use Erel's version of SearchView

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You will need to create a custom view for each item:
B4X:
Private Sub AddItemsToList(li As List, full As String)
   If li.IsInitialized = False Then Return
   Dim rs As RichString
   rs.Initialize("")
   For i = 0 To li.Size - 1
     Dim item As String
     item = li.Get(i)
     item = item.ToLowerCase
     If full.Length > MAX_LIMIT And item.Contains(full) = False Then
       Continue
     End If
     rs.Text = li.Get(i)
     If full.Length > 0 Then
       Dim ii As Int = item.IndexOf(full)
       rs.Color(Colors.Red, ii, ii + full.Length)
     End If
     Dim tc As TableCell = lv.AddSingleLine("")
     Dim p As Panel
     p.Initialize("")
     p.Color = Colors.Blue
     p.SetLayoutAnimated(0, 1, 0, 0, lv.Width, lv.RowHeight)
     Dim lbl As Label
     lbl.Initialize("")
     rs.SetToLabel(lbl)
     p.AddView(lbl, 5, 2, lv.Width, lv.RowHeight)
     tc.CustomView = p
     
   Next
   lv.ReloadAll
End Sub
Make sure to set lv.RowHeight = 30 in the Initialize sub.
 

Alvsky

Member
Licensed User
Longtime User
Thanks for the solution. It colors cells now but it has a bug. When I click on the item Value is empty.
It looks like ItemClick works only if tc.Text is set.
To make it work, I added tc.Text = rs.AttributedString and made lbl not Visible but I am not sure is that a right way to do it



B4X:
Private Sub AddItemsToList(li As List, full As String)
   If li.IsInitialized = False Then Return
   Dim rs As RichString
   rs.Initialize("")
   For i = 0 To li.Size - 1
     Dim item As String
     item = li.Get(i)
     item = item.ToLowerCase
     If full.Length > MAX_LIMIT And item.Contains(full) = False Then
       Continue
     End If
     rs.Text = li.Get(i)
     If full.Length > 0 Then
       Dim ii As Int = item.IndexOf(full)
       rs.Color(Colors.Red, ii, ii + full.Length)
     End If
     Dim tc As TableCell = lv.AddSingleLine("")
tc.Text = rs.AttributedString
     Dim p As Panel
     p.Initialize("")
     p.Color = Colors.Blue
     p.SetLayoutAnimated(0, 1, 0, 0, lv.Width, lv.RowHeight)
     Dim lbl As Label
     lbl.Initialize("")
lbl.Visible = False 
     rs.SetToLabel(lbl)
     p.AddView(lbl, 5, 2, lv.Width, lv.RowHeight)
     tc.CustomView = p
   Next
   lv.ReloadAll
End Sub
 
Top