Last active
March 17, 2022 07:52
-
-
Save bamatson/0b54a1a25eb6e4add40f to your computer and use it in GitHub Desktop.
Example solution to the StackOverflow question: http://stackoverflow.com/questions/26703211/android-autocomplete-matching-options
This file contains 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
public class DemoActivity extends Activity { | |
private String[] values = { | |
"Saudi Arabia", | |
"Aruba", | |
"Argentina", | |
"Something Else" | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_demo); | |
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); | |
SpecialAdapter adapter = new SpecialAdapter(this, android.R.layout.simple_dropdown_item_1line, values); | |
autoCompleteTextView.setAdapter(adapter); | |
} | |
public class SpecialAdapter extends BaseAdapter implements Filterable { | |
private List<String> originalItems; | |
private List<String> filteredItems; | |
private int layoutResource; | |
private Context context; | |
public SpecialAdapter(Context context, int resource, String[] items) { | |
this.context = context; | |
this.layoutResource = resource; | |
this.originalItems = Arrays.asList(items); | |
this.filteredItems = new ArrayList<String>(originalItems); | |
} | |
@Override | |
public int getCount() { | |
return filteredItems.size(); | |
} | |
@Override | |
public String getItem(int position) { | |
return filteredItems.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
String item = getItem(position); | |
if(convertView == null) { | |
convertView = inflater.inflate(layoutResource, parent, false); | |
} | |
((TextView) convertView).setText(item); | |
return convertView; | |
} | |
@Override | |
public Filter getFilter() { | |
return new SpecialFilter(); | |
} | |
private class SpecialFilter extends Filter { | |
@Override | |
protected FilterResults performFiltering(CharSequence constraint) { | |
FilterResults results = new FilterResults(); | |
// If there is no constraint, just return the original list of items | |
if (constraint == null || constraint.length() == 0) { | |
ArrayList<String> list = new ArrayList<String>(originalItems); | |
results.values = list; | |
results.count = list.size(); | |
} | |
// Else, only return the strings that start with the constraint | |
else { | |
String prefixString = constraint.toString().toLowerCase(); | |
ArrayList<String> values = new ArrayList<String>(originalItems); | |
final ArrayList<String> newValues = new ArrayList<String>(); | |
for (String value : values) { | |
final String valueText = value.toLowerCase(); | |
if (valueText.startsWith(prefixString)) { | |
newValues.add(value); | |
} | |
} | |
results.values = newValues; | |
results.count = newValues.size(); | |
} | |
return results; | |
} | |
@Override | |
protected void publishResults(CharSequence constraint, FilterResults results) { | |
filteredItems = (List<String>) results.values; | |
if (results.count > 0) { | |
notifyDataSetChanged(); | |
} else { | |
notifyDataSetInvalidated(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment