Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AutoCompleteCountryAdapter extends ArrayAdapter<Countries.CountriesData> {
- private List<Countries.CountriesData> countryListFull;
- public void setCountryListFull(List<Countries.CountriesData> countryListFull) {
- this.countryListFull = countryListFull;
- }
- public AutoCompleteCountryAdapter(@NonNull Context context, @NonNull List<Countries.CountriesData> countryList) {
- super(context, 0, countryList);
- countryListFull = new ArrayList<>(countryList);
- notifyDataSetChanged();
- }
- @NonNull
- @Override
- public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
- if (convertView == null) {
- convertView = LayoutInflater.from(getContext()).inflate(R.layout.country_autocomplete_row, parent, false);
- }
- TextView textViewName = convertView.findViewById(R.id.text_view_name);
- Countries.CountriesData countryItem = getItem(position);
- if (countryItem != null) {
- textViewName.setText(countryItem.getName());
- }
- return convertView;
- }
- @NonNull
- @Override
- public Filter getFilter(){
- return countryFilter;
- }
- private Filter countryFilter = new Filter() {
- @Override
- protected FilterResults performFiltering(CharSequence constraint) {
- FilterResults results = new FilterResults();
- List<Countries.CountriesData> suggestions = new ArrayList<>();
- if (constraint == null || constraint.length() == 0) {
- suggestions.addAll(countryListFull);
- } else {
- String filterPattern = constraint.toString().toLowerCase().trim();
- for (Countries.CountriesData item : countryListFull) {
- if (item.getName().toLowerCase().contains(filterPattern)) {
- suggestions.add(item);
- }
- }
- }
- results.values = suggestions;
- results.count = suggestions.size();
- return results;
- }
- @Override
- protected void publishResults(CharSequence constraint, FilterResults results) {
- clear();
- addAll((List) results.values);
- notifyDataSetChanged();
- }
- @Override
- public CharSequence convertResultToString(Object resultValue) {
- return ((Countries.CountriesData) resultValue).getName();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment