1: public class GameAdapter extends BaseAdapter implements Filterable {
2:
3: Context _context;
4: ArrayList<Game> games;
5:
6: public GameAdapter(Context context, ArrayList<Game> _games) {
7: _context = context;
8: games = _games;
9: orig = games;
10: filter = new GameFilter();
11: }
12:
13: @Override
14: public int getCount() {
15: if (games != null)
16: return games.size();
17: else
18: return 0;
19: }
20:
21: @Override
22: public Object getItem(int arg0) {
23: return games.get(arg0);
24: }
25:
26: @Override
27: public long getItemId(int arg0) {
28: return games.get(arg0).getID();
29: }
30:
31: @Override
32: public View getView(int arg0, View arg1, ViewGroup arg2) {
33: GameView gv;
34: if (arg1 == null)
35: gv = new GameView(_context, games.get(arg0));
36: else {
37: gv = (GameView) arg1;
38: gv.setID(games.get(arg0).getID());
39: gv.setName(games.get(arg0).getName());
40: }
41: return gv;
42: }
43:
44: @Override
45: public Filter getFilter() {
46: return filter;
47: }
48:
49: private GameFilter filter;
50: ArrayList<Game> orig;
51:
52: private class GameFilter extends Filter {
53:
54: public GameFilter() {
55:
56: }
57:
58: @Override
59: protected FilterResults performFiltering(CharSequence constraint) {
60: FilterResults oReturn = new FilterResults();
61: ArrayList<Game> results = new ArrayList<Game>();
62: if (orig == null)
63: orig = games;
64:
65: if (constraint != null)
66: {
67: if (orig != null && orig.size() > 0) {
68: for (Game g : orig) {
69: if (g.getName().contains(constraint))
70: results.add(g);
71: }
72: }
73: oReturn.values = results;
74: }
75: return oReturn;
76: }
77:
78: @SuppressWarnings("unchecked")
79: @Override
80: protected void publishResults(CharSequence constraint, FilterResults results) {
81: games = (ArrayList<Game>)results.values;
82: notifyDataSetChanged();
83: }
84: }
85:
86: }