1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.quicksearchbox.ui;
18
19import android.view.View;
20import android.view.ViewGroup;
21import android.widget.BaseAdapter;
22import android.widget.ListAdapter;
23
24import com.android.quicksearchbox.SuggestionCursor;
25import com.android.quicksearchbox.SuggestionPosition;
26import com.android.quicksearchbox.Suggestions;
27
28/**
29 * Uses a {@link Suggestions} object to back a {@link SuggestionsView}.
30 */
31public class SuggestionsListAdapter extends SuggestionsAdapterBase<ListAdapter> {
32
33    private Adapter mAdapter;
34
35    public SuggestionsListAdapter(SuggestionViewFactory viewFactory) {
36        super(viewFactory);
37        mAdapter = new Adapter();
38    }
39
40    @Override
41    public boolean isEmpty() {
42        return mAdapter.getCount() == 0;
43    }
44
45    @Override
46    public SuggestionPosition getSuggestion(long suggestionId) {
47        return new SuggestionPosition(getCurrentSuggestions(), (int) suggestionId);
48    }
49
50    @Override
51    public BaseAdapter getListAdapter() {
52        return mAdapter;
53    }
54
55    @Override
56    public void notifyDataSetChanged() {
57        mAdapter.notifyDataSetChanged();
58    }
59
60    @Override
61    public void notifyDataSetInvalidated() {
62        mAdapter.notifyDataSetInvalidated();
63    }
64
65    class Adapter extends BaseAdapter {
66
67        @Override
68        public int getCount() {
69            SuggestionCursor s = getCurrentSuggestions();
70            return s == null ? 0 : s.getCount();
71        }
72
73        @Override
74        public Object getItem(int position) {
75            return getSuggestion(position);
76        }
77
78        @Override
79        public long getItemId(int position) {
80            return position;
81        }
82
83        @Override
84        public View getView(int position, View convertView, ViewGroup parent) {
85            return SuggestionsListAdapter.this.getView(
86                    getCurrentSuggestions(), position, position, convertView, parent);
87        }
88
89        @Override
90        public int getItemViewType(int position) {
91            return getSuggestionViewType(getCurrentSuggestions(), position);
92        }
93
94        @Override
95        public int getViewTypeCount() {
96            return getSuggestionViewTypeCount();
97        }
98
99    }
100
101}
102