47
47
import java .util .ArrayList ;
48
48
import java .util .List ;
49
49
import java .util .Locale ;
50
+ import java .util .stream .Collectors ;
50
51
51
52
class ItemAdapter extends ArrayAdapter <Versioned <? extends Named >>
52
53
{
53
54
private final List <Versioned <? extends Named >> itemsAll ;
54
- private final List <Versioned <? extends Named >> suggestions ;
55
55
private final int viewResourceId ;
56
56
private List <String > tokens ;
57
57
@@ -60,44 +60,41 @@ public ItemAdapter(Context context, int viewResourceId, List<Versioned<? extends
60
60
super (context , viewResourceId , items );
61
61
62
62
this .itemsAll = new ArrayList <>(items );
63
- this .suggestions = new ArrayList <>();
64
63
this .viewResourceId = viewResourceId ;
65
64
}
66
65
67
66
@ Override
68
67
public View getView (int position , View convertView , ViewGroup parent )
69
68
{
70
69
View v = convertView ;
70
+
71
71
if (v == null )
72
72
{
73
73
LayoutInflater vi = (LayoutInflater ) getContext ().getSystemService (Context .LAYOUT_INFLATER_SERVICE );
74
74
v = vi .inflate (viewResourceId , null );
75
75
}
76
76
77
- if (position < suggestions .size ())
78
- {
79
- Versioned <? extends Named > item = suggestions .get (position );
77
+ final Versioned <? extends Named > item = getItem (position );
80
78
81
- TextView itemCaption = v .findViewById (R .id .itemDescription );
82
- itemCaption .setText (highlightOccurrences (
83
- item .getData ().getName (),
84
- tokens ,
85
- getContext ().getResources ().getColor (R .color .search_highlight , null )
86
- ));
79
+ TextView itemCaption = v .findViewById (R .id .itemDescription );
80
+ itemCaption .setText (highlightOccurrences (
81
+ item .getData ().getName (),
82
+ tokens ,
83
+ getContext ().getResources ().getColor (R .color .search_highlight , null )
84
+ ));
87
85
88
- // FIXME: use separated resources (not button's)
89
- if (item .getData () instanceof FoodItem )
90
- {
91
- itemCaption .setCompoundDrawablesWithIntrinsicBounds (R .drawable .button_foodbase , 0 , 0 , 0 );
92
- }
93
- else if (item .getData () instanceof DishItem )
94
- {
95
- itemCaption .setCompoundDrawablesWithIntrinsicBounds (R .drawable .button_dishbase , 0 , 0 , 0 );
96
- }
97
- else
98
- {
99
- throw new IllegalArgumentException ("Invalid item type: " + item .getClass ().getName ());
100
- }
86
+ // FIXME: use separated resources (not button's)
87
+ if (item .getData () instanceof FoodItem )
88
+ {
89
+ itemCaption .setCompoundDrawablesWithIntrinsicBounds (R .drawable .button_foodbase , 0 , 0 , 0 );
90
+ }
91
+ else if (item .getData () instanceof DishItem )
92
+ {
93
+ itemCaption .setCompoundDrawablesWithIntrinsicBounds (R .drawable .button_dishbase , 0 , 0 , 0 );
94
+ }
95
+ else
96
+ {
97
+ throw new IllegalArgumentException ("Invalid item type: " + item .getClass ().getName ());
101
98
}
102
99
103
100
return v ;
@@ -146,17 +143,12 @@ protected FilterResults performFiltering(CharSequence constraint)
146
143
if (constraint != null )
147
144
{
148
145
tokens = Utils .parseTokens (constraint .toString ());
149
- suggestions .clear ();
150
146
151
- for (Versioned <? extends Named > item : itemsAll )
152
- {
153
- if (Utils .matchesTokens (item .getData ().getName (), tokens ))
154
- {
155
- suggestions .add (item );
156
- }
157
- }
147
+ final List <Versioned <? extends Named >> suggestions = itemsAll .stream ()
148
+ .filter (e -> Utils .matchesTokens (e .getData ().getName (), tokens ))
149
+ .collect (Collectors .toList ());
158
150
159
- FilterResults filterResults = new FilterResults ();
151
+ final FilterResults filterResults = new FilterResults ();
160
152
filterResults .values = suggestions ;
161
153
filterResults .count = suggestions .size ();
162
154
0 commit comments