android - ArrayAdapter is updated late from Webservice in AutoCompleteTextAdapter -


i have autocompletetextview working arrayadapter. adapter updated webservice, when text changes. updated done in asynctask, supposed practice. working more or less, because suggestions after every key pressed based on strings retrieved in previous key pressed. there couple of problems related page, none of answers works me. anyway, had solution, inefficient , don't know why "official solution" fails.

i think key in function udpates de arrayadapter in background. in asynchronous call webservices:

private class doautocompletesearch extends asynctask<string, void, map<string, string>> {      @override     protected map<string, string> doinbackground(string... params) {          // ask webservice data         map<string, string> autocomplete = getresource.datalist(params[0]);         return autocomplete;     }      @override     protected void onpostexecute(map<string, string> result) {          //mautocompleteadapter.clear();    * should work not *  /* if set new adapter in autocompletetextview, whole thing works */      mautocompleteadapter = new arrayadapter<string>(mautocompleteadapter.getcontext(), android.r.layout.simple_dropdown_item_1line);     mactv.setadapter(mautocompleteadapter);          (map.entry<string, string> entry : result.entryset()) {             mautocompleteadapter.add(entry.getkey());         }     } } 

i have tried mautocompleteadapter.clear() , setting mautocompleteadapter.notifydatasetchanged() everywhere, useless.

i tried alot approach working, didn't succeed better yourself. found way accomplish want; extend arrayadapter , implement filterable. class doing actual fetching database, when called autocompletetextview in worker thread:

public class myautocompleteadapter extends arrayadapter<string> implements filterable {      private list<string> mdata = new arraylist<string>();     private server mserver;      public myautocompleteadapter(server server, context context, int textviewresourceid) {         super(context, textviewresourceid);         mserver = server;     }      @override     public int getcount() {         return mdata.size();     }      @override     public string getitem(int index) {         return mdata.get(index);     }      @override     public filter getfilter() {         filter myfilter = new filter() {             @override             protected filterresults performfiltering(charsequence constraint) {                 // method called in worker thread                  filterresults filterresults = new filterresults();                 if(constraint != null) {                     try {                         // here method (synchronous) fetches data                         // server                         list<string> results = mserver.searchforitems(constraint.tostring());                         filterresults.values = results;                         filterresults.count = results.size();                     }                     catch(exception e) {}                  }                 return filterresults;             }              @override             protected void publishresults(charsequence contraint, filterresults results) {                 if(results != null && results.count > 0) {                     mdata = (list<string>)results.values;                     notifydatasetchanged();                 }                 else {                     notifydatasetinvalidated();                 }             }         };         return myfilter;     } } 

edit: have improved above code since got exception java.lang.illegalstateexception: content of adapter has changed listview did not receive notification. fix this, moved updating of mdata publishresults().


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -