2011-11-23 45 views
3

以及我试图更新autoCompleteTextview ArrayAdapter dynamiclly每次通过使用TextWathcer更改文本。Android - AutoCompleteTextView动态更新问题

每当文本发生变化时,在新的AsyncTask中和来自异步任务(onPostExecute)的回调中,我从适配器调用clear()并向它添加新项目,然后调用notifyDataSetChanged。 不幸的是,自动完成下拉是从来没有显示..

请,我在哪里去worng?

这里是代码:

AutoCompleteTextView acCountry = (AutoCompleteTextView)layout.findViewById(R.id.autoComplete); 
final ArrayAdapter<RMAutoComplete.ListItem> countriesAdapter = new ArrayAdapter<RMAutoComplete.ListItem>(this.context,android.R.layout.simple_dropdown_item_1line); 
acCountry.addTextChangedListener(new TextWatcher() { 

      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       if (s.length() > 1) 
       { 
        new AsyncTask<String, Void, List<RMAutoComplete.ListItem>>(){ 

         @Override 
         protected List<ListItem> doInBackground(String... params) { 
          List<ListItem> l = null; 
          try { 
           l = location.getCountryData(params[0]); 
          } catch (Exception e) { 
           Log.e(TAG,"error when getCountryData",e); 
          } 
          return l; 
         } 

         @Override 
         protected void onPostExecute(List<ListItem> countries) { 
          countriesAdapter.clear(); 
          if (countries != null) 
           for (ListItem listItem : countries) { 
            countriesAdapter.add(listItem); 
           } 
          countriesAdapter.notifyDataSetChanged(); 
         } 
        }.execute(s.toString()); 
       } 
      } 

      public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

      public void afterTextChanged(Editable s) {} 
     } 
    ); 
+0

我正在做类似的事情! http://stackoverflow.com/questions/12854336/autocompletetextview-backed-by-cursorloader – toobsco42

回答

3

常见错误:你将适配器设置为acCountry呢?

+1

谢谢你!我不能相信这是我的错误.. 3小时!常见的错误。 :) – Adler