Question

I am trying to develop an app which updates an autocomplete view with json data from a webservice.

Using a textwatcher, I check in the ontextchanged callback and create an asynctask object which makes a non-blocking call to the webservice to get the results. When text is entered, any running asynctask objects are cancelled and a new one is generated( since an asynctask is a sort of singleton and I cannot rerun it).

The results are then added to the autocomplete view by clearing its ArrayAdapter and adding each new item.

The problem is that the data is almost never retrieved from the server, be it due to the constant cancels or whatever.

Is there correct solution for doing this, or has anybody ever achieved such a task succesfully?

Below are the relevant code snippets. TextWatcher,

public void onTextChanged(CharSequence s, int start, int before, int count) {
    Log.i("Text watcher", suggestsThread.getStatus().toString());
    if(s.length() >= suggestions.getThreshold() && suggestsThread.getStatus() != AsyncTask.Status.RUNNING) {
  suggestsThread.cancel(true);
  suggestsThread = new WertAgentThread();
  suggestsThread.execute(s.toString());
}

}

Updating the autocomplete task,

public void updateSuggestions(String[] suggestions) {
if( suggestions != null) {  
  try {
    for(int ctr = 0; ctr < suggestions.length; ctr++) {
      this.logger.append(suggestions[ctr]);
    }
    suggestAdapter = new ArrayAdapter<String>(this, R.layout.suggestions, suggestions);
    this.suggestions.setAdapter(suggestAdapter);
  } catch(NullPointerException ex) {
    Log.e("Updating adapter", ex.toString());
  }
}

The updateSuggestions is called in the onPostExecute callback in the Asynctask. Appreciating your help in advance,

Roland.

Was it helpful?

Solution

Forget I even asked this question. I discovered that the error was actually somewhere else, I was setting the http time out to milliseconds instead of seconds. Common mistakes :(.

For anybody trying to achieve the same, the method described above actually works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top