1cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/*
2cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Copyright (C) 2011 The Android Open Source Project
3cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
4cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * you may not use this file except in compliance with the License.
6cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You may obtain a copy of the License at
7cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
8cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn *
10cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Unless required by applicable law or agreed to in writing, software
11cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * See the License for the specific language governing permissions and
14cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * limitations under the License.
15cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
16cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
17cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpackage android.support.v4.widget;
18cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
19cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.Context;
20cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.database.Cursor;
21cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.net.Uri;
22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View;
23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.ImageView;
24cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.TextView;
25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/**
27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.widget.SimpleCursorAdapter}.
28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0.  When running
29cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try
30cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to switch to the framework's implementation.  See the framework SDK
31cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview.
32cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */
33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic class SimpleCursorAdapter extends ResourceCursorAdapter {
34cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * A list of columns containing the data to bind to the UI.
36cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
37cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected int[] mFrom;
40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * A list of View ids representing the views to which the data must be bound.
42cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This field should be made private, so it is hidden from the SDK.
43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@hide}
44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
45cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    protected int[] mTo;
46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private int mStringConversionColumn = -1;
48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private CursorToStringConverter mCursorToStringConverter;
49cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private ViewBinder mViewBinder;
50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    String[] mOriginalFrom;
52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Constructor the enables auto-requery.
55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @deprecated This option is discouraged, as it results in Cursor queries
57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * being performed on the application's UI thread and thus can cause poor
58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * responsiveness or even Application Not Responding errors.  As an alternative,
59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}.
60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Deprecated
62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super(context, layout, c);
64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mTo = to;
65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mOriginalFrom = from;
6691af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        findColumns(c, from);
67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
68cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
69cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
70cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Standard constructor.
71cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
72cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param context The context where the ListView associated with this
73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            SimpleListItemFactory is running
74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param layout resource identifier of a layout file that defines the views
75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            for this list item. The layout file should include at least
76cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            those named views defined in "to"
77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param c The database cursor.  Can be null if the cursor is not available yet.
78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param from A list of column names representing the data to bind to the UI.  Can be null
79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            if the cursor is not available yet.
80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param to The views that should display column in the "from" parameter.
81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            These should all be TextViews. The first N views in this list
82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            are given the values of the first N columns in the from
83cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            parameter.  Can be null if the cursor is not available yet.
84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param flags Flags used to determine the behavior of the adapter,
85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}.
86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from,
88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            int[] to, int flags) {
89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super(context, layout, c, flags);
90cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mTo = to;
91cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mOriginalFrom = from;
9291af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        findColumns(c, from);
93cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
94cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
95cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
96cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Binds all of the field names passed into the "to" parameter of the
97cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * constructor with their corresponding cursor columns as specified in the
98cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * "from" parameter.
99cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Binding occurs in two phases. First, if a
101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link android.widget.SimpleCursorAdapter.ViewBinder} is available,
102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link ViewBinder#setViewValue(android.view.View, android.database.Cursor, int)}
103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is invoked. If the returned value is true, binding has occured. If the
104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * returned value is false and the view to bind is a TextView,
105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #setViewText(TextView, String)} is invoked. If the returned value is
106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * false and the view to bind is an ImageView,
107cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * {@link #setViewImage(ImageView, String)} is invoked. If no appropriate
108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * binding can be found, an {@link IllegalStateException} is thrown.
109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @throws IllegalStateException if binding cannot occur
111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
11291af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * @see android.widget.CursorAdapter#bindView(View, Context, Cursor)
113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getViewBinder()
11491af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * @see #setViewBinder(ViewBinder)
115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setViewImage(ImageView, String)
116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setViewText(TextView, String)
117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void bindView(View view, Context context, Cursor cursor) {
120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        final ViewBinder binder = mViewBinder;
121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        final int count = mTo.length;
122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        final int[] from = mFrom;
123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        final int[] to = mTo;
124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        for (int i = 0; i < count; i++) {
126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            final View v = view.findViewById(to[i]);
127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (v != null) {
128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                boolean bound = false;
129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (binder != null) {
130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    bound = binder.setViewValue(v, cursor, from[i]);
131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (!bound) {
134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    String text = cursor.getString(from[i]);
135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (text == null) {
136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        text = "";
137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (v instanceof TextView) {
140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        setViewText((TextView) v, text);
141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    } else if (v instanceof ImageView) {
142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        setViewImage((ImageView) v, text);
143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    } else {
144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                " view that can be bounds by this SimpleCursorAdapter");
146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Returns the {@link ViewBinder} used to bind data to views.
154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return a ViewBinder or null if the binder does not exist
156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #bindView(android.view.View, android.content.Context, android.database.Cursor)
15891af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * @see #setViewBinder(ViewBinder)
159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public ViewBinder getViewBinder() {
161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mViewBinder;
162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Sets the binder used to bind data to views.
166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param viewBinder the binder used to bind data to views, can be null to
168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *        remove the existing binder
169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #bindView(android.view.View, android.content.Context, android.database.Cursor)
171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getViewBinder()
172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setViewBinder(ViewBinder viewBinder) {
174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mViewBinder = viewBinder;
175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Called by bindView() to set the image for an ImageView but only if
179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * there is no existing ViewBinder or if the existing ViewBinder cannot
180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * handle binding to an ImageView.
181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * By default, the value will be treated as an image resource. If the
183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * value cannot be used as an image resource, the value is used as an
184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * image Uri.
185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Intended to be overridden by Adapters that need to filter strings
187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * retrieved from the database.
188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param v ImageView to receive an image
190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param value the value retrieved from the cursor
191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setViewImage(ImageView v, String value) {
193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        try {
194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            v.setImageResource(Integer.parseInt(value));
195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } catch (NumberFormatException nfe) {
196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            v.setImageURI(Uri.parse(value));
197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Called by bindView() to set the text for a TextView but only if
202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * there is no existing ViewBinder or if the existing ViewBinder cannot
20391af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * handle binding to a TextView.
204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Intended to be overridden by Adapters that need to filter strings
206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * retrieved from the database.
207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param v TextView to receive text
209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param text the text to be set for the TextView
210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setViewText(TextView v, String text) {
212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        v.setText(text);
213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Return the index of the column used to get a String representation
217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * of the Cursor.
218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return a valid index in the current Cursor or -1
220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setStringConversionColumn(int)
22391af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * @see #setCursorToStringConverter(CursorToStringConverter)
224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getCursorToStringConverter()
225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int getStringConversionColumn() {
227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mStringConversionColumn;
228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Defines the index of the column in the Cursor used to get a String
232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * representation of that Cursor. The column is used to convert the
233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Cursor to a String only when the current CursorToStringConverter
234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is null.
235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param stringConversionColumn a valid index in the current Cursor or -1 to use the default
237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *        conversion mechanism
238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getStringConversionColumn()
24191af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * @see #setCursorToStringConverter(CursorToStringConverter)
242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getCursorToStringConverter()
243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setStringConversionColumn(int stringConversionColumn) {
245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStringConversionColumn = stringConversionColumn;
246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Returns the converter used to convert the filtering Cursor
250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * into a String.
251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return null if the converter does not exist or an instance of
253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *         {@link android.widget.SimpleCursorAdapter.CursorToStringConverter}
254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
25591af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * @see #setCursorToStringConverter(CursorToStringConverter)
256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getStringConversionColumn()
257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setStringConversionColumn(int)
258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public CursorToStringConverter getCursorToStringConverter() {
261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mCursorToStringConverter;
262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Sets the converter  used to convert the filtering Cursor
266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * into a String.
267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cursorToStringConverter the Cursor to String converter, or
269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *        null to remove the converter
270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
27191af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * @see #setCursorToStringConverter(CursorToStringConverter)
272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getStringConversionColumn()
273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setStringConversionColumn(int)
274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setCursorToStringConverter(CursorToStringConverter cursorToStringConverter) {
277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mCursorToStringConverter = cursorToStringConverter;
278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Returns a CharSequence representation of the specified Cursor as defined
282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * by the current CursorToStringConverter. If no CursorToStringConverter
283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * has been set, the String conversion column is used instead. If the
284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * conversion column is -1, the returned String is empty if the cursor
285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is null or Cursor.toString().
286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cursor the Cursor to convert to a CharSequence
288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return a non-null CharSequence representing the cursor
290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public CharSequence convertToString(Cursor cursor) {
293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mCursorToStringConverter != null) {
294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return mCursorToStringConverter.convertToString(cursor);
295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else if (mStringConversionColumn > -1) {
296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return cursor.getString(mStringConversionColumn);
297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return super.convertToString(cursor);
300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
30391af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * Create a map from an array of strings to an array of column-id integers in cursor c.
30491af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * If c is null, the array will be discarded.
30591af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     *
30691af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * @param c the cursor to find the columns from
307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param from the Strings naming the columns of interest
308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
30991af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette    private void findColumns(Cursor c, String[] from) {
31091af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        if (c != null) {
311cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            int i;
312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            int count = from.length;
313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (mFrom == null || mFrom.length != count) {
314cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mFrom = new int[count];
315cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            for (i = 0; i < count; i++) {
31791af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette                mFrom[i] = c.getColumnIndexOrThrow(from[i]);
318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else {
320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            mFrom = null;
321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
322cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
324cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public Cursor swapCursor(Cursor c) {
32691af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        // super.swapCursor() will notify observers before we have
32791af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        // a valid mapping, make sure we have a mapping before this
32891af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        // happens
32991af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        findColumns(c, mOriginalFrom);
33091af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        return super.swapCursor(c);
331cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
33291af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette
333cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Change the cursor and change the column-to-view mappings at the same time.
33591af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     *
336cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param c The database cursor.  Can be null if the cursor is not available yet.
337cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param from A list of column names representing the data to bind to the UI.  Can be null
338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            if the cursor is not available yet.
339cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param to The views that should display column in the "from" parameter.
340cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            These should all be TextViews. The first N views in this list
341cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            are given the values of the first N columns in the from
342cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            parameter.  Can be null if the cursor is not available yet.
343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
344cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void changeCursorAndColumns(Cursor c, String[] from, int[] to) {
345cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mOriginalFrom = from;
346cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mTo = to;
34791af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        // super.changeCursor() will notify observers before we have
34891af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        // a valid mapping, make sure we have a mapping before this
34991af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        // happens
35091af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        findColumns(c, mOriginalFrom);
35191af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette        super.changeCursor(c);
352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This class can be used by external clients of SimpleCursorAdapter
356cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to bind values fom the Cursor to views.
357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * You should use this class to bind values from the Cursor to views
359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * that are not directly supported by SimpleCursorAdapter or to
360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * change the way binding occurs for views supported by
361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * SimpleCursorAdapter.
362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
36391af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette     * @see SimpleCursorAdapter#bindView(View, Context, Cursor)
364cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see SimpleCursorAdapter#setViewImage(ImageView, String)
365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see SimpleCursorAdapter#setViewText(TextView, String)
366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
36791af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette    public interface ViewBinder {
368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Binds the Cursor column defined by the specified index to the specified view.
370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * When binding is handled by this ViewBinder, this method must return true.
372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * If this method returns false, SimpleCursorAdapter will attempts to handle
373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * the binding on its own.
374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param view the view to bind the data to
376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param cursor the cursor to get the data from
377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param columnIndex the column at which the data can be found in the cursor
378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @return true if the data was bound to the view, false otherwise
380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        boolean setViewValue(View view, Cursor cursor, int columnIndex);
382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This class can be used by external clients of SimpleCursorAdapter
386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to define how the Cursor should be converted to a String.
387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
39091af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette    public interface CursorToStringConverter {
391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Returns a CharSequence representing the specified Cursor.
393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param cursor the cursor for which a CharSequence representation
395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *        is requested
396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @return a non-null CharSequence representing the cursor
398cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        CharSequence convertToString(Cursor cursor);
400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
403