Wednesday 26 September 2012

android: AutoCompleteTextView with addTextChangedListener


 public class CountriesActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.countries);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter); 
textView.addTextChangedListener(new TextWatcher() {
    
 @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
 if(adapter.getCount() == 0) {
Toast.makeText(getApplicationContext(), "Adapter : 0", Toast.LENGTH_SHORT).show();
    }
     
  }
    
 @Override
public void beforeTextChanged(CharSequence s, int start, int count,
      int after) {
// TODO Auto-generated method stub
     
    }
    
   
@Override
public void afterTextChanged(Editable s) {
     
    }
   });
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain"
     };
 }

No comments:

Post a Comment