KickingLettuce KickingLettuce - 6 months ago 36
SQL Question

AutoCompleteTextView with MySQL Data

I have an

AutoCompleteTextView
that I need to suggest names from a
MySQL
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
AsyncTask
. I know how to handle the
AsynTask
portion, but here are my questions:

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

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

Answer

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.

Pseudo-code:

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);
       actv.setAdapter(adp);
   }
}

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.

Comments