Search Remote Database

squaremation

Active Member
Licensed User
Longtime User
RESOLVED SORT OF SEE BELOW:
Been trying to solve this problem, there are alot of examples on the forums, but non of them have seemed to work in this situation. Them seem to be all local db.

I am attempting a 'Dictionary Type' app. I want to user to be able to search with a edittext view or search bar.


I can load the 'words' my db into a ListView, and when a field is clicked get the definition to load from the db.

This works fine to click on listview of Words:
B4X:
Sub ListView1_ItemClick (Position As Int, Value As Object)
   Dim tl As TwoLines
   tl = Value
   lblWord.Text = tl.First
   lblDefinition.Text = "Contacting Server..."
   ExecuteRemoteQuery("SELECT definition FROM dictionary WHERE id='" & tl.Second & "'", WORD_DEFINITION)
End Sub

I dont see why I can't us a string variable from the user's input in a EditText view or the Android Search Bar, and putting it the query doesn't work. Trying to figure out why this does not work. (Logs show blank [] server response),:THIS DOES WORK IF USED AS BUTTON_CLICK AND NOT EDITTEXT_ENTERPRESSED
B4X:
Sub EditText1_EnterPressed
   Dim usrInPut As String 
   usrInPut = EditText1.Text
   lblDefinition.Text = "Contacting server..."
   ExecuteRemoteQuery("SELECT definition FROM dictionary WHERE id='" & usrInPut & "'", WORD_DEFINITION)
End Sub

This is the ExcecuteRemoteQuery code if that matters, pretty much based off Erel's MySql example :
B4X:
Sub ExecuteRemoteQuery(Query As String, TaskId As Int)
   Dim req As HttpRequest
   req.InitializePost2("http://mysite.myserver.com/", Query.GetBytes("UTF8"))
   hc.Execute(req, TaskId)
End Sub
 
Last edited:

squaremation

Active Member
Licensed User
Longtime User
Are you calling the PHP script from the MySQL example?
Yes, not at the same address, but yes the exact same PHP script
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I'm afraid I don't know much about remote access but to answer your second question, you can access the elements in a listview via its GetItem method, and if the items in it are sorted, you could use a binary chop algorithm to speed up the search.

Although I have found it more convenient to keep an index in a separate list, then you can use the lists indexof method directly. I also use the list as a basis for a filter.

Edit: or you could look at the standard AutoCompleteEditText and see if that does what you want.
 
Last edited:
Upvote 0

squaremation

Active Member
Licensed User
Longtime User
I'm afraid I don't know much about remote access but to answer your second question, you can access the elements in a listview via its GetItem method, and if the items in it are sorted, you could use a binary chop algorithm to speed up the search.

Although I have found it more convenient to keep an index in a separate list, then you can use the lists indexof method directly. I also use the list as a basis for a filter.

Edit: or you could look at the standard AutoCompleteEditText and see if that does what you want.

thnx stevel

I am attempting to learn this to try and implement it in my app. I don't understandwhy a a db cant be searched from a search box, but I will try this solution with index and sorting.
 
Upvote 0

squaremation

Active Member
Licensed User
Longtime User
:sign0060:
This will work as button click for the query not sure why enterPress wont work:
Also works with corwin42 example of Native Search bar

B4X:
Sub Button2_click
   Dim i As String 
   i = EditText1.Text
   lblWord.Text = i 
   lblDefinition.Text = "Loading from Dictionary server..."
   ExecuteRemoteQuery("SELECT definition FROM dictionary WHERE id='" & i & "'", WORD_DEFINITION)
End Sub
 
Last edited:
Upvote 0
Top