Reputation: 16790
I have a list of cities(around 2500 of them). I want the user to get auto suggestions when he types in the edittextbox from that list. How is this achieved? I have searched Google through and through but cannot find any tutorial about this. Would really appreciate any help. Thanks.
Upvotes: 0
Views: 1709
Reputation: 216
You can use "Filter" for searching from your other list. Implement your own filter [MyFilter extends Filter] and override "FilterResults performFiltering(CharSequence prefix)" and "void publishResults(CharSequence constraint,FilterResults results)" methods to implement your own searching logic on your own data (country list).
Add a getMyFilter() method in your adapter which will return a "new myFilter" Add a "TextWatcher" to your "EditText" Inside "onTextChanged()" method of the TextWatcher, get your filter by calling myAdapter.getMyFilter()". On that filter, call "myFilter.filter(myTypedString)".
After filtering "void publishResults(CharSequence constraint,FilterResults results)" method will get called. Inside that, change your actual adapter data to refresh your UI.
Upvotes: 0
Reputation: 3382
Use AutoCompleteTextview, here is a tutorial from the development site (bottom of the page). http://developer.android.com/guide/topics/ui/controls/text.html
Upvotes: 1
Reputation: 25143
You should use AutoCompleteTextView to achieve this.
The following code snippet shows how to create a text view which suggests various countries names while the user is typing:
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);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
Upvotes: 2