Search listview

magarcan

Active Member
Licensed User
Longtime User
I need to use a Dialog Search Box to find items in my listview. Somethink like this:
RMR1TVB6yaUAbmz-Sbu7wbfZss-CDT44CJX8Cw_VPEUXH13SZOiYgVoB_85h-aPkA0w


Any idea about how to do this?
 

margret

Well-Known Member
Licensed User
Longtime User
This is code I use to search through an SQL data file and fill the ListView. I use it in the TextChanged event of the EditTextSearch field.

B4X:
Sub EditTextSearch_TextChanged (Old As String, New As String)
   If s.Len(s.Trim(New)) < 1 Then
      ListViewSearch.Clear
      Return
   End If
   holdp = pointer.Position
   thm = pointer.RowCount - 1
   Dim LVlbl As Label
   LVlbl = ListViewSearch.SingleLineLayout.Label
   LVlbl.Color = Colors.Gray
   ListViewSearch.Clear
   If RBnames.Checked Then
      For l = 0 To thm
         pointer.Position = l
         seekvar = pointer.GetString ("name")            
         If s.At(s.Upper(seekvar), s.Upper(New)) > -1 Then
            ListViewSearch.AddSingleLine(seekvar)
            ListViewSearch.Invalidate
            DoEvents
         End If
      Next
   End If
   If RBnotes.Checked Then
      For l = 0 To thm
         pointer.Position = l
         seekvar = pointer.GetString ("enotesee")            
         If s.At(s.Upper(seekvar), s.Upper(New)) > -1 Then
            ListViewSearch.AddSingleLine(pointer.GetString ("name"))
            ListViewSearch.Invalidate
            DoEvents
         End If
      Next
   End If
   pointer.Position = holdp
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
This is code I use to search through an SQL data file and fill the ListView. I use it in the TextChanged event of the EditTextSearch field.

B4X:
Sub EditTextSearch_TextChanged (Old As String, New As String)
   If s.Len(s.Trim(New)) < 1 Then
      ListViewSearch.Clear
      Return
   End If
   holdp = pointer.Position
   thm = pointer.RowCount - 1
   Dim LVlbl As Label
   LVlbl = ListViewSearch.SingleLineLayout.Label
   LVlbl.Color = Colors.Gray
   ListViewSearch.Clear
   If RBnames.Checked Then
      For l = 0 To thm
         pointer.Position = l
         seekvar = pointer.GetString ("name")            
         If s.At(s.Upper(seekvar), s.Upper(New)) > -1 Then
            ListViewSearch.AddSingleLine(seekvar)
            ListViewSearch.Invalidate
            DoEvents
         End If
      Next
   End If
   If RBnotes.Checked Then
      For l = 0 To thm
         pointer.Position = l
         seekvar = pointer.GetString ("enotesee")            
         If s.At(s.Upper(seekvar), s.Upper(New)) > -1 Then
            ListViewSearch.AddSingleLine(pointer.GetString ("name"))
            ListViewSearch.Invalidate
            DoEvents
         End If
      Next
   End If
   pointer.Position = holdp
End Sub

Margret, I usually prefer to query the db instead of looping through contents, and then populate the results. But I don't know if this is really faster. Just a thought :)
 
Upvote 0

squaremation

Active Member
Licensed User
Longtime User
Margret, I usually prefer to query the db instead of looping through contents, and then populate the results. But I don't know if this is really faster. Just a thought :)



What I am trying to figure out is how did you get this in the list.

unnamed.png


I should just have to figure this out.
B4X:
    Dim LVlbl As Label
    LVlbl = ListViewSearch.SingleLineLayout.Label
    LVlbl.Color = Colors.Gray
    ListViewSearch.Clear
    If RBnames.Checked Then
        For l = 0 To thm
            pointer.Position = l
            seekvar = pointer.GetString ("name")                
            If s.At(s.Upper(seekvar), s.Upper(New)) > -1 Then
                ListViewSearch.AddSingleLine(seekvar)
                ListViewSearch.Invalidate
                DoEvents
            End If
        Next
    End If




.
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
He is asking about the categories in the list. I.e. the headers a,b,c,d...

Sent from my GT-I9000 using Tapatalk 2

I think this can be done by firstly sorting the list, then populating the listview. Whenever you find a different letter in the beginning of the word, simply add this letter in the list. You can even use a letter as image, to make it look better.
Of course, I think that a scrollview might be more efficient, but I really don't know :)
Here's an example I've just made, you can find it attached.
 

Attachments

  • sortedlistview.zip
    56.7 KB · Views: 442
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
On mine I used two listview. One with A-Z on the righthand side. Then I populate the main listview with all entries. Then if the user selects from the righthand list any letter a-z, it clears the left list and reloads it with items that start with the selected letter. Seems to work good with what I tested up to around 800 entries. See attached image.
 

Attachments

  • Untitled-1.jpg
    Untitled-1.jpg
    48.5 KB · Views: 394
Last edited:
Upvote 0

squaremation

Active Member
Licensed User
Longtime User
:icon_clap:

Here's an example I've just made, you can find it attached.

Worked perfect with a little mod because my db is remote.

:D
On mine I used two listview. One with A-Z on the righthand side. Then I populate the main listview with all entries. Then if the user selects from the righthand list any letter a-z, it clears the left list and reloads it with items that start with the selected letter. Seems to work good with what I tested up to around 800 entries. See attached image.

Thnx I'm digging into this now!
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime User
On mine I used two listview. One with A-Z on the righthand side. Then I populate the main listview with all entries. Then if the user selects from the righthand list any letter a-z, it clears the left list and reloads it with items that start with the selected letter. Seems to work good with what I tested up to around 800 entries. See attached image.

Hello, Margret.

Yours is a good solid basic solution to a common problem - indexing listviews. And sometimes, basic is best.

In my solution, tested with 1700 entries, I do not reload the list. I just do the following:

B4X:
ListView.SetSelection

When I first load the table, I create an index by letter which I use to set the listview.setselection.

Thank you for all of your contributions to the forum.

Best regards.

Sandy
 
Upvote 0
Top