android - ArrayAdapter need to be clear even i am creating a new one -


hello i'm having problems understanding how arrayadapter works. code working dont know how.(http://amy-mac.com/images/2013/code_meme.jpg)

i have activity, inside have 2 private classes.:

public class mainactivity extends activity {     ...     private void someprivatemethod(){         autocompletetextview.setadapter(new arrayadapter<string>(this,             android.r.layout.simple_spinner_dropdown_item, new arraylist<string>(arrays.aslist(""))));         autocompletetextview.addtextchangedlistener(new mytextwatcher());     }     ...      private class mytextwatcher implements textwatcher {         ...     }     private class searchaddresstask extends asynctask<string, void, string[]> {         ...     } } 

now inside textwatcher class call search address task:

@override public void aftertextchanged(editable s) {     new searchaddresstask().execute(s.tostring()); } 

so far good.

in searchaddresstask stuff on doinbackground() returns right array.

on onpostexecute() method try modify autocompletetextview adapter add values array obtained in doinbackground() adapter cannot modified:

not working code:

protected void onpostexecute(string[] addressarray) {     arrayadapter<string> adapter = (arrayadapter<string>) autocompletedestination.getadapter();     adapter.clear();     adapter.addall(new arraylist<string>(arrays.aslist(addressarray)));     adapter.notifydatasetchanged();     log.d("searchaddresstask", "adapter isempty : " + adapter.isempty()); // returns true!!??! } 

i dont why not working. if run on ui thread...

i kept investigating, if recreate arrayadapter, working in ui (showing suggestions), still need clear old adapter:

working code:

protected void onpostexecute(string[] addressarray) {     arrayadapter<string> adapter = (arrayadapter<string>) autocompletedestination.getadapter();     adapter.clear();     autocompletedestination.setadapter(new arrayadapter<string>(newdestinationactivity.this,android.r.layout.simple_spinner_dropdown_item, new arraylist<string>(arrays.aslist(addressarray))));     //adapter.notifydatasetchanged(); // no needed     log.d("searchaddresstask", "adapter isempty : " + adapter.isempty()); // keeps returning true!!??! } 

so question is, happening arrayadapter? why cannot modify in onpostexecute()? why working in ui if recreating adapter? , why need clear old adapter then?

i dont know there many questions need in here!!

thanks!!

i followed recomendation of pskink, lot!!

i switched responsability arrayadapter , implemented own filter.

import java.util.arraylist;  import android.content.context; import android.widget.arrayadapter; import android.widget.filter; import android.widget.filterable;  import com.roisoftstudio.travelalarm.gui.utils.streetlisthelper;  public class autocompleteadapter extends arrayadapter<string> implements filterable {     private arraylist<string> mdata;     private context context;      public autocompleteadapter(context context, int textviewresourceid) {         super(context, textviewresourceid);         this.context = context;         mdata = new arraylist<string>();     }      @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) {                 filterresults filterresults = new filterresults();                 if (constraint != null) {                     mdata = streetlisthelper.getstreetlist(context, constraint.tostring());                     filterresults.values = mdata;                     filterresults.count = mdata.size();                 }                 return filterresults;             }              @override             protected void publishresults(charsequence constraint, filterresults results) {             }          };         return myfilter;     } } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -