Android Question Playing a Query in Listview

MarcioCC

Member
Licensed User
Longtime User
Good evening, my friends have the following query:

MyQuery="SELECT * FROM PRODUTOS WHERE CODBARRAS LIKE 'a' "
Cursor1=SQL1.ExecQuery(MyQuery)

I have a view (edittext) how do I find a record in my table by typing an information in (edittext) that information appears in listview ??
Does anyone have an example ??
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I think with that query, all you'll get is records where CODBARRAS = 'a'. You need something like:

B4X:
    Cursor1=SQL1.ExecQuery("SELECT * FROM PRODUTOS WHERE CODBARRAS LIKE '%a%'") 'or 'a%' if you only want records that start with 'a' or '%a' if you only want records that end with 'a'

For displaying results in a ListView based on a search string typed into an EditText, I guess something like:

B4X:
    Private Sub EditText_TextChanged (Old As String, New As String)
        Private i as Int

        ListView.Clear
        Private Cursor1 as Cursor = SQL1.ExecQuery($"SELECT * FROM PRODUTOS WHERE CODBARRAS LIKE '%${New}%'"$)
        For i = 0 to Cursor1.RowCount - 1
            Cursor1.Position = i
            ListView.Add(Cursor1.GetString("CODBARRAS"))
        Next
    End Sub

Although I suspect that this might be a bit "clunky" to the user if you are executing a query every time they type a letter...

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
I guess the other option would be to run a query where you load all the records in the table into your cursor, then search the cursor for whatever they type into the EditText.

- Colin.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…