Android Question Listview - find specific item

tdocs2

Well-Known Member
Licensed User
Greetings, all.

Thank you in advance for answering my question.

Case
1. Listview of names sorted by last name
2. Enter on keyboard (external is a more practical example), "Jennings" (without the quotes) and the listview will scroll to nearest match.

Question
How do I accomplish this?
SearchView would address this need, but I want to keep the listview visible to give the user the ability to scroll up or down.

I am certain the question has been asked, but I could not find it in the forum.

Best regards.

Sandy
 
Last edited:

tdocs2

Well-Known Member
Licensed User
Thank you, Mahares.

I was hoping for a trivial solution - like I missed something in Listview, capture the keystrokes in Activity_Key.... and then set Listview.

I will have to study the SearchView Class and test it. I am appreciative in your pointing the specific posts in the SearchView thread. At times, it is difficult to find specific information in long threads.

Best regards.

Sandy
 

Dman

Active Member
Licensed User
I have a personal app that I use an edittext to type the name into and add this on the text changed event. It will narrow the search for every letter typed into it. I only use this for my personal use and haven't really tried to "break" it since it works for me so there might need to be a little tweaking on it.

B4X:
ListView1.Clear
   dbcursor = SQL1.ExecQuery("SELECT name FROM attendee WHERE name LIKE '%" & txtAttendee.Text & "%'")
  
   For I = 0 To dbcursor.RowCount - 1
     DoEvents
       dbcursor.Position = I
       ListView1.AddSingleLine(dbcursor.GetString("name"))
     Next  
    
   dbcursor.Close
 

Mahares

Well Known Member
Licensed User
ListView1.Clear
dbcursor = SQL1.ExecQuery(
"SELECT name FROM attendee WHERE name LIKE '%" & txtAttendee.Text & "%'")

For I = 0 To dbcursor.RowCount - 1
DoEvents
dbcursor.Position = I
ListView1.AddSingleLine(dbcursor.GetString(
"name"))
Next

dbcursor.Close
That can be quite tedious if you have a big list as you are recreating the query and rebuilding the listview every time you add or remove a letter from the edittext box. The SearchView is based on the same concept you are using except that you are building the index only once, thus much more efficient. If you are dealing with long lists, you may want to consider it in lieu of what you are currently using.
 

Dman

Active Member
Licensed User
Like I said, I use it only for an app for myself with a very limited database. I will check out the searchview though, thanks.
 

klaus

Expert
Licensed User
B4X:
For I = 0 To dbcursor.RowCount - 1
    DoEvents
    dbcursor.Position = I
    ListView1.AddSingleLine(dbcursor.GetString("name"))
Next
Why do you use DoEvents in the For / Next loop?
It will increae the search time.
 

Dman

Active Member
Licensed User
Don't remember my thinking on that. That particular app was written a while back and like I said, it only searches around 60 or so names the one time a year that I use it.
 
Top