19
19
import android .view .View ;
20
20
import android .view .ViewGroup ;
21
21
import android .widget .BaseAdapter ;
22
+ import android .widget .Filter ;
23
+ import android .widget .Filterable ;
24
+
25
+ import java .util .Collections ;
26
+ import java .util .List ;
22
27
23
28
/**
24
29
* List adapter for items of a specific type
27
32
* @param <V>
28
33
*/
29
34
public abstract class ItemListAdapter <I , V extends ItemView > extends
30
- BaseAdapter {
35
+ BaseAdapter implements Filterable {
31
36
32
37
private final LayoutInflater inflater ;
33
38
34
39
private final int viewId ;
35
40
36
- private Object [] elements ;
41
+ private List <I > items ;
42
+
43
+ private List <I > initialItems ;
37
44
38
45
/**
39
46
* Create empty adapter
@@ -50,16 +57,17 @@ public ItemListAdapter(final int viewId, final LayoutInflater inflater) {
50
57
*
51
58
* @param viewId
52
59
* @param inflater
53
- * @param elements
60
+ * @param items
54
61
*/
55
62
public ItemListAdapter (final int viewId , final LayoutInflater inflater ,
56
- final I [] elements ) {
63
+ final List < I > items ) {
57
64
this .viewId = viewId ;
58
65
this .inflater = inflater ;
59
- if (elements != null )
60
- this .elements = elements ;
66
+ if (items != null )
67
+ this .items = items ;
61
68
else
62
- this .elements = new Object [0 ];
69
+ this .items = Collections .emptyList ();
70
+ this .initialItems = this .items ;
63
71
}
64
72
65
73
@ Override
@@ -68,37 +76,60 @@ public boolean hasStableIds() {
68
76
}
69
77
70
78
/**
79
+ * Get items being displayed
80
+ *
71
81
* @return items
72
82
*/
73
- @ SuppressWarnings ("unchecked" )
74
- protected I [] getItems () {
75
- return (I []) elements ;
83
+ public List <I > getItems () {
84
+ return items ;
76
85
}
77
86
78
87
public int getCount () {
79
- return elements . length ;
88
+ return items . size () ;
80
89
}
81
90
82
- @ SuppressWarnings ("unchecked" )
83
91
public I getItem (int position ) {
84
- return ( I ) elements [ position ] ;
92
+ return items . get ( position ) ;
85
93
}
86
94
87
95
public long getItemId (int position ) {
88
- return elements [position ].hashCode ();
96
+ return getItem (position ).hashCode ();
97
+ }
98
+
99
+ /**
100
+ * @return initialItems
101
+ */
102
+ protected List <I > getInitialItems () {
103
+ return initialItems ;
89
104
}
90
105
91
106
/**
92
107
* Set items
93
108
*
94
109
* @param items
95
- * @return items
110
+ * @return this adapter
111
+ */
112
+ public ItemListAdapter <I , V > setItems (final List <I > items ) {
113
+ if (items != null )
114
+ this .items = items ;
115
+ else
116
+ this .items = Collections .emptyList ();
117
+ initialItems = this .items ;
118
+ notifyDataSetChanged ();
119
+ return this ;
120
+ }
121
+
122
+ /**
123
+ * Set filtered items to display
124
+ *
125
+ * @param items
126
+ * @return this adapter
96
127
*/
97
- public ItemListAdapter <I , V > setItems (final Object [] items ) {
128
+ public ItemListAdapter <I , V > setFilteredItems (final List < I > items ) {
98
129
if (items != null )
99
- elements = items ;
130
+ this . items = items ;
100
131
else
101
- elements = new Object [ 0 ] ;
132
+ this . items = Collections . emptyList () ;
102
133
notifyDataSetChanged ();
103
134
return this ;
104
135
}
@@ -120,6 +151,7 @@ public ItemListAdapter<I, V> setItems(final Object[] items) {
120
151
*/
121
152
protected abstract V createView (View view );
122
153
154
+ @ Override
123
155
public View getView (final int position , View convertView ,
124
156
final ViewGroup parent ) {
125
157
@ SuppressWarnings ("unchecked" )
@@ -132,4 +164,9 @@ public View getView(final int position, View convertView,
132
164
update (position , view , getItem (position ));
133
165
return convertView ;
134
166
}
167
+
168
+ @ Override
169
+ public Filter getFilter () {
170
+ return null ;
171
+ }
135
172
}
0 commit comments