Skip to content

Instantly share code, notes, and snippets.

@caneryilmaz
Last active September 13, 2018 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caneryilmaz/9379f788bc7a212491b6c2818dd5ccdb to your computer and use it in GitHub Desktop.
Save caneryilmaz/9379f788bc7a212491b6c2818dd5ccdb to your computer and use it in GitHub Desktop.
/**
* Created by Caner on 3.01.2018.
*/
``` public class SectorSuggestionAdapter extends ArrayAdapter<Sektorler> {
private List<Sektorler> items, tempItems, suggestions;
private Context context;
public SectorSuggestionAdapter(Context context, int textViewResourceId, List<Sektorler> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.items = objects;
this.tempItems = new ArrayList<>(objects);
this.suggestions = new ArrayList<Sektorler>();
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.layout_tag_text, null);
}
Sektorler sektor = items.get(position);
if (sektor != null) {
TextView tt = v.findViewById(R.id.text_city_name);
tt.setText(sektor.getSektorAdi());
}
return v;
}
@NonNull
@Override
public Filter getFilter() {
return sektorFilter;
}
/**
* Custom Filter implementation for custom suggestions we provide.
*/
Filter sektorFilter = new Filter() {
@Override
public CharSequence convertResultToString(Object resultValue) {
String str = ((Sektorler) resultValue).getSektorAdi();
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if (constraint != null) {
suggestions.clear();
for (Sektorler sektorler : tempItems) {
if (sektorler.getSektorAdi().toLowerCase().startsWith(constraint.toString().toLowerCase())) {
suggestions.add(sektorler);
} else if (sektorler.getSektorAdi().toLowerCase().contains(constraint.toString().toLowerCase())) {
suggestions.add(sektorler);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
List<Sektorler> filterList = (ArrayList<Sektorler>) results.values;
if (results != null && results.count > 0) {
clear();
try {
for (Sektorler sektorler : filterList) {
add(sektorler);
notifyDataSetChanged();
}
} catch (Exception ex) {
}
}
}
};
}```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment