Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 2.19 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. AsyncTask:onPostExecute
  2. _artist = (AutoCompleteTextView)findViewById(R.id.artist);  
  3.   _artist.addTextChangedListener(new TextWatcher()
  4.     {
  5.         public void afterTextChanged(Editable s)
  6.         {
  7.             _artist.setAdapter(null);
  8.             _fetcher = new AutoCompleteFetcher(_artist);
  9.             _fetcher.execute();        
  10.         }
  11.     }
  12.  
  13.     public class AutoCompleteFetcher extends AsyncTask<Void, Void, Void>
  14.     {      
  15.         private AutoCompleteTextView _textView;
  16.         private String[] _matches;
  17.  
  18.         public AutoCompleteFetcher(AutoCompleteTextView v)
  19.         {
  20.             super();
  21.             _textView = v;
  22.         }
  23.  
  24.         protected Void doInBackground(Void... v)
  25.         {  
  26.             _matches = _getMatches();              
  27.             return null;
  28.         }
  29.  
  30.         private String[] _getMatches()
  31.         {  
  32.             // fill the list....... code removed here
  33.             // returns populated String[]
  34.         }
  35.  
  36.         @Override
  37.         protected void onPostExecute(Void result)
  38.         {  
  39.             ArrayAdapter<String> adapter =
  40.                     new ArrayAdapter<String>(_textView.getContext(),
  41.                         android.R.layout.simple_list_item_1,_matches);
  42.             _textView.setAdapter(adapter);
  43.         }
  44. }
  45.        
  46. public class AutoCompleteFetcher extends AsyncTask<Void, Void, String[]>
  47. {      
  48.     private AutoCompleteTextView _textView;
  49.  
  50.     public AutoCompleteFetcher(AutoCompleteTextView v)
  51.     {
  52.         super();
  53.         _textView = v;
  54.     }
  55.  
  56.     @Override
  57.     protected String[] doInBackground(Void... v)
  58.     {  
  59.         return _getMatches();              
  60.     }
  61.  
  62.     private String[] _getMatches()
  63.     {  
  64.         // fill the list....... code removed here
  65.         // returns populated String[]
  66.         // TODO: it seems like this method should really just be moved into
  67.         //  doInBackground().  That said, I don't think this is your problem.
  68.     }
  69.  
  70.     @Override
  71.     protected void onPostExecute(String[] result)
  72.     {  
  73.         ArrayAdapter<String> adapter =
  74.                 new ArrayAdapter<String>(_textView.getContext(),
  75.                     android.R.layout.simple_list_item_1, result);
  76.         _textView.setAdapter(adapter);
  77.     }
  78. }