KickingLettuce KickingLettuce - 1 year ago 130
SQL Question

AutoCompleteTextView with MySQL Data

I have an

that I need to suggest names from a
table. I have only once used this before with hard coding a
string array

I have only seen one example, at this link, and it really wasn't much of a help. I need to do this with an
. I know how to handle the
portion, but here are my questions:

I'm going to have to send each part of the phrase typed as a
for the
. Where do I get this text

The only method I can see is a
maybe. Would that be it, or is there another method?

Answer Source

Would that be it, or is there another method?

You simply need some mechanism that will "watch" changes in your inputbox and the most correct way how to achieve it is mentioned TextWatcher

So implement it and in some method that provides TextWatcher for instance onTextChanged(), assign data from inputbox and send them as parameter to AsyncTask and in onPostExecute() method create new Adapter for your AutoCompleteTextView with data retrieved from MySQL and assign Adapter to your widget and you got it.


public void onTextChanged(CharSequence s, int start, int before, int count) {
   if (s.length() > 1) {
      insertString = s.toString();
      new YourTask().execute(insertString);                  

and in your AsyncTask, perform something like this:

protected List<String> doInBackground() {
   // fetchning data from MySQL
   return list;

public void onPostExecute(List<String> result) {
   if (!result.isEmpty()) {
       SomeAdapter adp = new SomeAdapter(context, layout, result);

Note: In your case is easer to make your AsyncTask inner class of your Activity class and you have direct access to UI components without passing them via constructor.