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,094
Last edited:

madSac

Active Member
Licensed User
Longtime User
is there any method to load two items.Like contact name and phone number.
 

pixelpop

Active Member
Licensed User
Longtime User
Erel,

A user asked about using a delimited list, searching the first delimited item and returning the second. You said it would entail a change to the class.

Support for two delimited items would be very useful. In my case, I have a list of airlines and their codes:

British Airways,BA
British Airways Shuttle,SHT
British International,BS
British North West Airlines,BNW
Brussels Airlines,SN
Brussels International Airlines,BXI
Budapest Aircraft Service,BPS
Buddha Air,BHA

I need to display the airline name and return the airline code. Other uses might be name,email_addr or city,zipcode. Can you point me towards where the class mods need to be made to display only the first item and return the second item? Thanks.
 

pixelpop

Active Member
Licensed User
Longtime User
Thanks for the help, Erel. I have loaded the csv file using:

csv = su.LoadCSV(File.DirAssets, "all_airlines.txt", ",")

Could you provide a line of code that will load the first items in csv into SearchView? I can manage the rest. Thanks!

UPDATE: I didn't realize that a String variable could be an array. Quite a departure from VB, but all is good now. :)
 
Last edited:

Tadder

Member
Licensed User
Longtime User
loading 2 SV on 1 panel

Hey Erel - I'm loading two SearchView's from a MySQL server and for some reason it's populating both with the same data set.

here is the code, what's interesting is the DB data that gets loaded is the 2nd query statement... meaning if you switch the order (put conn first, then mfr) both lists show mfr records. the way it is written below shows the conn records, not the mfr.

any help would be appreciated!
B4X:
mfrSV.Initialize(Me,"mfrSV")
   Dim mfr As List = Main.serverDB.Query("SELECT mfrNum FROM products")
   mfrSV.AddToParent(pnl_searchconn,51%x,8%y,100%x,98%y)
   
   connSV.Initialize(Me,"connSV")
   Dim conn As List = Main.serverDB.Query("SELECT prodID FROM products")
   connSV.AddToParent(pnl_searchconn,2%x,8%y,48%x,100%y)
   
   If FirstTime Then
      mfrPNforSearch = mfrSV.SetItems(mfr)
      connPNforSearch = connSV.SetItems(conn)
   Else
      mfrSV.SetIndex(mfrPNforSearch)
      connSV.SetIndex(connPNforSearch)
   End If
 

Tadder

Member
Licensed User
Longtime User
nothing showed up in the Log... weird. maybe because the variables are lists, and not strings?

but in the debugger, the local variables did show that they were the same lists.

Could you clarify what you mean for me please,

I recommend you to use the web service approach for your code.
and also what you mean by
Blocking the main thread will cause problems.
 

ikidd

Member
Licensed User
Longtime User
I love this class, but I have issue with it when I've used it and then the operation panel comes up based on the picked item. If I try to go back and pick another item, I can type the new search term, and while I can't see the items list, I can still pick a line by tapping below.

I'm sure it's something with background or foreground colors, but I can't see how I can get handles on the editText or the Listview that is inside SearchView to manipulate them and make them opaque.

attachment.php


Once the ops panel is up, if I change the Searchview text, the items aren't visible through the ops panel, but can be chosen.

attachment.php


How do I get the textbox handle and how do I put the list on top when it's active?
 

Attachments

  • Screenshot_2013-02-28-20-28-09.png
    Screenshot_2013-02-28-20-28-09.png
    31.1 KB · Views: 610
  • Screenshot_2013-02-28-20-29-04.png
    Screenshot_2013-02-28-20-29-04.png
    50.2 KB · Views: 620

Erel

B4X founder
Staff member
Licensed User
Longtime User
@Tadder, it seems like you are using a library that blocks the main thread. This will not work reliably. You should instead use a web service based solution as demonstrated in the MySQL or MS SQL tutorials.

@ikidd, the problem if I understand correctly is that your panel hides SearchView panel. Why don't you hide this panel when the user enters any text in the SearchView field?
 

Tadder

Member
Licensed User
Longtime User
i don't understand what you mean by "blocks the main thread"... could you please explain, or point me in the right direction?

i did get it to work, btw, just had to load the DB's one at a time..:
B4X:
'LOAD THE FIRST LIST
   mfrSV.Initialize(Me,"mfrSV")
   Dim mfr As List = Main.serverDB.Query("SELECT mfrNum FROM products")
   mfrSV.AddToParent(pnl_searchconn,51%x,8%y,100%x,98%y)
   If FirstTime Then
      mfrPNforSearch = mfrSV.SetItems(mfr)
   Else
      mfrSV.SetIndex(mfrPNforSearch)
   End If
'LOAD THE SECOND LIST
   connSV.Initialize(Me,"connSV")
   Dim conn As List = Main.serverDB.Query("SELECT prodID FROM products")
   connSV.AddToParent(pnl_searchconn,2%x,8%y,48%x,100%y)
   If FirstTime Then
      connPNforSearch = connSV.SetItems(conn)
   Else
      connSV.SetIndex(connPNforSearch)
   End If
one issue though, it's pretty slow. suppose this has something to do with not using the web service you are describing?
 

ikidd

Member
Licensed User
Longtime User
Fair enough on the hide the panel.

How do I grab the EditText handle inside the sv so I can reset it if the user doesn't choose a valid option?
 

Tadder

Member
Licensed User
Longtime User
So I switched out what I was using for the web service you told me to run. works 100 times better, thanks for that! i had looked at it at first a few weeks ago, but didn't understand it, and the one i was using was pretty simple (it seemed). which leads me to my next question...

how do i use the searchview with a remote MySQL connection? i'm getting really confused about what happens when i call ExecuteRemoteQuery as far as what happens, and when it happens. for example:
B4X:
connSV.Initialize(Me,"connSV")
connSV.AddToParent(pnl_searchconn,51%x,8%y,100%x,98%y)
query = "SELECT prodID from PRODUCTS"
ExecuteRemoteQuery(query,"LoadSVparts")
If FirstTime Then
   connPNforSearch = connSV.SetItems(partslist)
Else
   connSV.SetIndex(connPNforSearch)
End If
fyi, partslist is a global list that gets set in JobDone.

This is erroring out because it gets to connSV.SetItems before it gets to initializing everything. how do i control the timing?
 

Tadder

Member
Licensed User
Longtime User
so here's my temporary fix, unless i don't get a suggestion, or figure it out myself. i was trying to load two SV's side-by-side - one with my p/n's and one with mfr p/n's. i don't understand the timing issues, but for the time being, i'm just grabbing both columns from the remote DB at the same time and then loading them into only one SV. like this (this code is in the JobDone Sub):
B4X:
For i = 0 To SVParts.Size-1
   partsMap = SVParts.Get(i)
   partslist.Add(partsMap.Get("prodID"))
   partslist.Add(partsMap.Get("mfrNUM"))
Next
connPNforSearch = connSV.SetItems(partslist)
problem is this doubles the size of the list (now totals 12000+ items), thus doubling the time it takes to initialize it... plus it has to re-do this everytime the user logs in (which i probably want to have happen anyway, as we'll be updating the DB daily :cool:). Using this method is OK for now, and it may not get any better.

One other thing - it already takes about 10seconds to load (Building index... msg is up for about that long), and that is on Wifi with a 10meg cable modem... so i'm a little worried about someone that is connecting on 3G or worse...

is there any way to speed up the loading time?
 
Last edited:

sanjibnanda

Active Member
Licensed User
Longtime User
search view with two views

hello!

i tried to populate two search view in same activity as

B4X:
sv.Initialize(Me, "sv")
   
sv1.Initialize(Me, "sv1")

it worked well,
now i want to display different hint/control to edit box like

B4X:
et.Hint="Delhi"

but it display and pass control to both sv..and sv1 simultaneously

how can two sv be handled separately with their independent control..plz advice

sanjib
 
Status
Not open for further replies.
Top