2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.widget;
19 import android.content.Context;
20 import android.database.ContentObserver;
21 import android.database.Cursor;
22 import android.database.DataSetObserver;
23 import android.os.Handler;
24 import android.util.Config;
25 import android.util.Log;
26 import android.view.View;
27 import android.view.ViewGroup;
30 * Adapter that exposes data from a {@link android.database.Cursor Cursor} to a
31 * {@link android.widget.ListView ListView} widget. The Cursor must include
32 * a column named "_id" or this class will not work.
34 public abstract class CursorAdapter extends BaseAdapter implements Filterable,
35 CursorFilter.CursorFilterClient {
37 * This field should be made private, so it is hidden from the SDK.
40 protected boolean mDataValid;
42 * This field should be made private, so it is hidden from the SDK.
45 protected boolean mAutoRequery;
47 * This field should be made private, so it is hidden from the SDK.
50 protected Cursor mCursor;
52 * This field should be made private, so it is hidden from the SDK.
55 protected Context mContext;
57 * This field should be made private, so it is hidden from the SDK.
60 protected int mRowIDColumn;
62 * This field should be made private, so it is hidden from the SDK.
65 protected ChangeObserver mChangeObserver;
67 * This field should be made private, so it is hidden from the SDK.
70 protected DataSetObserver mDataSetObserver = new MyDataSetObserver();
72 * This field should be made private, so it is hidden from the SDK.
75 protected CursorFilter mCursorFilter;
77 * This field should be made private, so it is hidden from the SDK.
80 protected FilterQueryProvider mFilterQueryProvider;
83 * Constructor. The adapter will call requery() on the cursor whenever
84 * it changes so that the most recent data is always displayed.
86 * @param c The cursor from which to get the data.
87 * @param context The context
89 public CursorAdapter(Context context, Cursor c) {
90 init(context, c, true);
95 * @param c The cursor from which to get the data.
96 * @param context The context
97 * @param autoRequery If true the adapter will call requery() on the
98 * cursor whenever it changes so the most recent
99 * data is always displayed.
101 public CursorAdapter(Context context, Cursor c, boolean autoRequery) {
102 init(context, c, autoRequery);
105 protected void init(Context context, Cursor c, boolean autoRequery) {
106 boolean cursorPresent = c != null;
107 mAutoRequery = autoRequery;
109 mDataValid = cursorPresent;
111 mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1;
112 mChangeObserver = new ChangeObserver();
114 c.registerContentObserver(mChangeObserver);
115 c.registerDataSetObserver(mDataSetObserver);
120 * Returns the cursor.
121 * @return the cursor.
123 public Cursor getCursor() {
128 * @see android.widget.ListAdapter#getCount()
130 public int getCount() {
131 if (mDataValid && mCursor != null) {
132 return mCursor.getCount();
139 * @see android.widget.ListAdapter#getItem(int)
141 public Object getItem(int position) {
142 if (mDataValid && mCursor != null) {
143 mCursor.moveToPosition(position);
151 * @see android.widget.ListAdapter#getItemId(int)
153 public long getItemId(int position) {
154 if (mDataValid && mCursor != null) {
155 if (mCursor.moveToPosition(position)) {
156 return mCursor.getLong(mRowIDColumn);
166 public boolean hasStableIds() {
171 * @see android.widget.ListAdapter#getView(int, View, ViewGroup)
173 public View getView(int position, View convertView, ViewGroup parent) {
175 throw new IllegalStateException("this should only be called when the cursor is valid");
177 if (!mCursor.moveToPosition(position)) {
178 throw new IllegalStateException("couldn't move cursor to position " + position);
181 if (convertView == null) {
182 v = newView(mContext, mCursor, parent);
186 bindView(v, mContext, mCursor);
191 public View getDropDownView(int position, View convertView, ViewGroup parent) {
193 mCursor.moveToPosition(position);
195 if (convertView == null) {
196 v = newDropDownView(mContext, mCursor, parent);
200 bindView(v, mContext, mCursor);
208 * Makes a new view to hold the data pointed to by cursor.
209 * @param context Interface to application's global information
210 * @param cursor The cursor from which to get the data. The cursor is already
211 * moved to the correct position.
212 * @param parent The parent to which the new view is attached to
213 * @return the newly created view.
215 public abstract View newView(Context context, Cursor cursor, ViewGroup parent);
218 * Makes a new drop down view to hold the data pointed to by cursor.
219 * @param context Interface to application's global information
220 * @param cursor The cursor from which to get the data. The cursor is already
221 * moved to the correct position.
222 * @param parent The parent to which the new view is attached to
223 * @return the newly created view.
225 public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
226 return newView(context, cursor, parent);
230 * Bind an existing view to the data pointed to by cursor
231 * @param view Existing view, returned earlier by newView
232 * @param context Interface to application's global information
233 * @param cursor The cursor from which to get the data. The cursor is already
234 * moved to the correct position.
236 public abstract void bindView(View view, Context context, Cursor cursor);
239 * Change the underlying cursor to a new cursor. If there is an existing cursor it will be
242 * @param cursor the new cursor to be used
244 public void changeCursor(Cursor cursor) {
245 if (cursor == mCursor) {
248 if (mCursor != null) {
249 mCursor.unregisterContentObserver(mChangeObserver);
250 mCursor.unregisterDataSetObserver(mDataSetObserver);
254 if (cursor != null) {
255 cursor.registerContentObserver(mChangeObserver);
256 cursor.registerDataSetObserver(mDataSetObserver);
257 mRowIDColumn = cursor.getColumnIndexOrThrow("_id");
259 // notify the observers about the new cursor
260 notifyDataSetChanged();
264 // notify the observers about the lack of a data set
265 notifyDataSetInvalidated();
270 * <p>Converts the cursor into a CharSequence. Subclasses should override this
271 * method to convert their results. The default implementation returns an
272 * empty String for null values or the default String representation of
275 * @param cursor the cursor to convert to a CharSequence
276 * @return a CharSequence representing the value
278 public CharSequence convertToString(Cursor cursor) {
279 return cursor == null ? "" : cursor.toString();
283 * Runs a query with the specified constraint. This query is requested
284 * by the filter attached to this adapter.
286 * The query is provided by a
287 * {@link android.widget.FilterQueryProvider}.
288 * If no provider is specified, the current cursor is not filtered and returned.
290 * After this method returns the resulting cursor is passed to {@link #changeCursor(Cursor)}
291 * and the previous cursor is closed.
293 * This method is always executed on a background thread, not on the
294 * application's main thread (or UI thread.)
296 * Contract: when constraint is null or empty, the original results,
297 * prior to any filtering, must be returned.
299 * @param constraint the constraint with which the query must be filtered
301 * @return a Cursor representing the results of the new query
304 * @see #getFilterQueryProvider()
305 * @see #setFilterQueryProvider(android.widget.FilterQueryProvider)
307 public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
308 if (mFilterQueryProvider != null) {
309 return mFilterQueryProvider.runQuery(constraint);
315 public Filter getFilter() {
316 if (mCursorFilter == null) {
317 mCursorFilter = new CursorFilter(this);
319 return mCursorFilter;
323 * Returns the query filter provider used for filtering. When the
324 * provider is null, no filtering occurs.
326 * @return the current filter query provider or null if it does not exist
328 * @see #setFilterQueryProvider(android.widget.FilterQueryProvider)
329 * @see #runQueryOnBackgroundThread(CharSequence)
331 public FilterQueryProvider getFilterQueryProvider() {
332 return mFilterQueryProvider;
336 * Sets the query filter provider used to filter the current Cursor.
338 * {@link android.widget.FilterQueryProvider#runQuery(CharSequence)}
339 * method is invoked when filtering is requested by a client of
342 * @param filterQueryProvider the filter query provider or null to remove it
344 * @see #getFilterQueryProvider()
345 * @see #runQueryOnBackgroundThread(CharSequence)
347 public void setFilterQueryProvider(FilterQueryProvider filterQueryProvider) {
348 mFilterQueryProvider = filterQueryProvider;
352 * Called when the {@link ContentObserver} on the cursor receives a change notification.
353 * The default implementation provides the auto-requery logic, but may be overridden by
356 * @see ContentObserver#onChange(boolean)
358 protected void onContentChanged() {
359 if (mAutoRequery && mCursor != null && !mCursor.isClosed()) {
360 if (Config.LOGV) Log.v("Cursor", "Auto requerying " + mCursor + " due to update");
361 mDataValid = mCursor.requery();
365 private class ChangeObserver extends ContentObserver {
366 public ChangeObserver() {
367 super(new Handler());
371 public boolean deliverSelfNotifications() {
376 public void onChange(boolean selfChange) {
381 private class MyDataSetObserver extends DataSetObserver {
383 public void onChanged() {
385 notifyDataSetChanged();
389 public void onInvalidated() {
391 notifyDataSetInvalidated();