Neyasbit

CompleteAdapter

Apr 13th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. public class AutoCompleteCountryAdapter extends ArrayAdapter<Countries.CountriesData> {
  2.  
  3.     private List<Countries.CountriesData> countryListFull;
  4.  
  5.     public void setCountryListFull(List<Countries.CountriesData> countryListFull) {
  6.         this.countryListFull = countryListFull;
  7.     }
  8.  
  9.     public AutoCompleteCountryAdapter(@NonNull Context context, @NonNull List<Countries.CountriesData> countryList) {
  10.         super(context, 0, countryList);
  11.         countryListFull = new ArrayList<>(countryList);
  12.         notifyDataSetChanged();
  13.     }
  14.  
  15.     @NonNull
  16.     @Override
  17.     public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  18.         if (convertView == null) {
  19.             convertView = LayoutInflater.from(getContext()).inflate(R.layout.country_autocomplete_row, parent, false);
  20.         }
  21.  
  22.          TextView textViewName = convertView.findViewById(R.id.text_view_name);
  23.          Countries.CountriesData countryItem = getItem(position);
  24.  
  25.         if (countryItem != null) {
  26.             textViewName.setText(countryItem.getName());
  27.         }
  28.         return convertView;
  29.     }
  30.  
  31.     @NonNull
  32.     @Override
  33.     public Filter getFilter(){
  34.         return countryFilter;
  35.     }
  36.  
  37.     private Filter countryFilter = new Filter() {
  38.         @Override
  39.         protected FilterResults performFiltering(CharSequence constraint) {
  40.             FilterResults results = new FilterResults();
  41.  
  42.             List<Countries.CountriesData> suggestions = new ArrayList<>();
  43.             if (constraint == null || constraint.length() == 0) {
  44.                 suggestions.addAll(countryListFull);
  45.             } else {
  46.  
  47.                 String filterPattern = constraint.toString().toLowerCase().trim();
  48.  
  49.                 for (Countries.CountriesData item : countryListFull) {
  50.                     if (item.getName().toLowerCase().contains(filterPattern)) {
  51.                         suggestions.add(item);
  52.                     }
  53.                 }
  54.             }
  55.  
  56.             results.values = suggestions;
  57.             results.count = suggestions.size();
  58.             return results;
  59.         }
  60.  
  61.         @Override
  62.         protected void publishResults(CharSequence constraint, FilterResults results) {
  63.             clear();
  64.             addAll((List) results.values);
  65.             notifyDataSetChanged();
  66.         }
  67.  
  68.         @Override
  69.         public CharSequence convertResultToString(Object resultValue) {
  70.             return ((Countries.CountriesData) resultValue).getName();
  71.         }
  72.     };
  73. }
Advertisement
Add Comment
Please, Sign In to add comment