-
-
Save anonymous/af1ba137069de32367c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.HashMap; | |
| import java.util.Map; | |
| import android.app.Activity; | |
| import android.os.Bundle; | |
| import android.util.Log; | |
| import android.widget.ArrayAdapter; | |
| import android.widget.AutoCompleteTextView; | |
| import android.widget.Filter; | |
| public class CountriesActivity extends Activity { | |
| private static final String TAG = "CountriesActivity"; | |
| private AutoCompleteTextView mCountriesAutocompleteTextView; | |
| private AutoCompleteTextView mCitiesAutoCompleteTextView; | |
| protected void onCreate(Bundle icicle) { | |
| super.onCreate(icicle); | |
| setContentView(R.layout.activity_countries); | |
| mCountriesAutocompleteTextView = (AutoCompleteTextView) findViewById(R.id.countries_list); | |
| mCitiesAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.cities_list); | |
| ArrayAdapter<String> countriesAdapter = new ArrayAdapter<String>(this, | |
| android.R.layout.simple_dropdown_item_1line, COUNTRIES); | |
| ArrayAdapter<String> citiesAdapter = new ArrayAdapter<String>(this, | |
| android.R.layout.simple_dropdown_item_1line) { | |
| @Override | |
| public Filter getFilter() { | |
| // TODO Auto-generated method stub | |
| return new Filter() { | |
| @Override | |
| protected void publishResults(CharSequence constraint, | |
| FilterResults results) { | |
| Log.d(TAG, "Publishing w/" + results.count); | |
| String[] filteredList = (String[]) results.values; | |
| if (results != null && results.count > 0) { | |
| Log.d(TAG, "Trying to publish " + results.count); | |
| clear(); | |
| for (String c : filteredList) { | |
| add(c); | |
| } | |
| notifyDataSetChanged(); | |
| } | |
| } | |
| @Override | |
| protected FilterResults performFiltering( | |
| CharSequence constraint) { | |
| Log.d(TAG, constraint.toString()); | |
| String selectedCountry = mCountriesAutocompleteTextView | |
| .getText().toString(); | |
| String[] cities = CountriesActivity.getCities().get( | |
| selectedCountry); | |
| if (constraint != null | |
| && CountriesActivity.getCities().containsKey( | |
| selectedCountry)) { | |
| Log.d(TAG, "Found"); | |
| FilterResults filterResults = new FilterResults(); | |
| filterResults.values = cities; | |
| filterResults.count = cities.length; | |
| return filterResults; | |
| } else { | |
| return new FilterResults(); | |
| } | |
| } | |
| }; | |
| } | |
| }; | |
| mCountriesAutocompleteTextView.setAdapter(countriesAdapter); | |
| mCitiesAutoCompleteTextView.setAdapter(citiesAdapter); | |
| } | |
| private static final String[] COUNTRIES = new String[] { "France", | |
| "Germany" }; | |
| private static final Map<String, String[]> getCities() { | |
| Map<String, String[]> cities = new HashMap<String, String[]>(); | |
| String[] french = { "Paris", "Lyon", "Versailles" }; | |
| String[] german = { "Berlin", "Munich", "Nuremburg" }; | |
| cities.put("France", french); | |
| cities.put("Germany", german); | |
| return cities; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment