0

I have an app where there is a AutoCompleteTextView. On every text change event, the app goes on to the web to retrieve some content from the internet and populates the dropdown for the TextView. I have used AsyncTask for doing the web content read. However, if new text is typed before the content is received and populated, the app is hanging till the old content is fetched. Is there a way to get around the problem?

My AsyncTask is as follows,

private class GetSuggestions extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        System.out.println("Suggestions Called()");
        doSearch(params[0]);  // reads the web and populates the suggestions ArrayList
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        System.out.println("Adapter Called() " + suggestions.size());
        suggestionAdapter = new ArrayAdapter<String>(
                getApplicationContext(), R.layout.list, suggestions);
        searchText.setAdapter(suggestionAdapter);
    }
}

Thx! Rahul.

rahul
  • 6,447
  • 3
  • 31
  • 42
  • In your case it depends on what you are trying to accomplish. I would say if whatever is in the dropdown is directly related to whatever the `TextView` is, and its always that one `TextView`, then stop the process and restart it with the new one. If not, then you could always make a waiting list and have `onPostExecute` check that list to see if anything is next. – Andy Jul 31 '12 at 05:30

5 Answers5

0

You can check if the AsyncTask is running or not.

public boolean isRunning()
{
    if (_querymysqltask == null) return false;
    if (_querymysqltask.getStatus() == AsyncTask.Status.FINISHED) return false;
    else return true;
}

You can cancel the task to restart it with a new search, or wait it ends.

chrisendymion
  • 175
  • 1
  • 9
0
if(task == null)
{
    task = new GetSuggestions();
    task.execute(new String[] {word});
}
else
{
    task.cancel(true);
    task = new GetSuggestions();
    task.execute(new String[] {word});
}

You can cancel the task and start a new one with the new entered text.The code will like something above.

Murat
  • 3,084
  • 37
  • 55
0

You can show progressDialog till the data is fetched from web.

   private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);

    /** progress dialog to show user that the backup is processing. */
    /** application context. */

    protected void onPreExecute() {
        this.dialog.setMessage("Please wait");
        this.dialog.show();
    }


    @Override
    protected void onPostExecute(final Boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }
Nirali
  • 13,571
  • 6
  • 40
  • 53
0

You probably want to cancel the AsyncTask if the user enters new text. Canceling an AsyncTask is described in the following dos.

http://developer.android.com/reference/android/os/AsyncTask.html

tamsler
  • 1,275
  • 1
  • 18
  • 26
0

It seems like problem is in

doSearch(params[0]);  // reads the web and populates the suggestions ArrayList

doSearch() has been called from doInBackground(), so it is not supposed to touch the UI elemnets.only, do the 'read the web' part from doInBackground() and populate the ArrayList from onPostExecute().

Eight
  • 4,194
  • 5
  • 30
  • 51