Advertisement
teguhsugiono891208

PeopleAdapter

Nov 6th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. public class PeopleAdapter extends ArrayAdapter<People> {
  2. Context context;
  3. int resource, textViewResourceId;
  4. List<People> items, tempItems, suggestions;
  5.  
  6. public PeopleAdapter(Context context, int resource, int textViewResourceId, List<People> items) {
  7. super(context, resource, textViewResourceId, items);
  8. this.context = context;
  9. this.resource = resource;
  10. this.textViewResourceId = textViewResourceId;
  11. this.items = items;
  12. tempItems = new ArrayList<People>(items); // this makes the difference.
  13. suggestions = new ArrayList<People>();
  14. }
  15.  
  16. @Override
  17. public View getView(int position, View convertView, ViewGroup parent) {
  18. View view = convertView;
  19. if (convertView == null) {
  20. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  21. view = inflater.inflate(R.layout.row_people, parent, false);
  22. }
  23.  
  24. People people = items.get(position);
  25. if (people != null) {
  26. TextView lblName = (TextView) view.findViewById(R.id.lbl_name);
  27. if (lblName != null)
  28. lblName.setText(people.getName());
  29. }
  30.  
  31. return view;
  32. }
  33.  
  34. @Override
  35. public Filter getFilter() {
  36. return nameFilter;
  37. }
  38.  
  39. /**
  40. * Custom Filter implementation for custom suggestions we provide.
  41. */
  42. Filter nameFilter = new Filter() {
  43. @Override
  44. public CharSequence convertResultToString(Object resultValue) {
  45. //String str = ((People) resultValue).getName() +" <---> "+ ((People) resultValue).getId();
  46. String str = ((People) resultValue).getName() ;
  47. return str;
  48. }
  49.  
  50. @Override
  51. protected FilterResults performFiltering(CharSequence constraint) {
  52. if (constraint != null) {
  53. suggestions.clear();
  54. for (People people : tempItems) {
  55. if (people.getName().toLowerCase().contains(constraint.toString().toLowerCase())) {
  56. suggestions.add(people);
  57. }
  58. }
  59. FilterResults filterResults = new FilterResults();
  60. filterResults.values = suggestions;
  61. filterResults.count = suggestions.size();
  62. return filterResults;
  63. } else {
  64. return new FilterResults();
  65. }
  66. }
  67.  
  68. @Override
  69. protected void publishResults(CharSequence constraint, FilterResults results) {
  70. List<People> filterList = (ArrayList<People>) results.values;
  71. if (results != null && results.count > 0) {
  72. clear();
  73. for (People people : filterList) {
  74. add(people);
  75. notifyDataSetChanged();
  76. }
  77. }
  78. }
  79. };
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement