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;
66cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        findColumns(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;
92cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        findColumns(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     *
112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#bindView(android.view.View,
113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *      android.content.Context, android.database.Cursor)
114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getViewBinder()
115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setViewBinder(android.widget.SimpleCursorAdapter.ViewBinder)
116cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setViewImage(ImageView, String)
117cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setViewText(TextView, String)
118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
119cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void bindView(View view, Context context, Cursor cursor) {
121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        final ViewBinder binder = mViewBinder;
122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        final int count = mTo.length;
123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        final int[] from = mFrom;
124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        final int[] to = mTo;
125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        for (int i = 0; i < count; i++) {
127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            final View v = view.findViewById(to[i]);
128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            if (v != null) {
129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                boolean bound = false;
130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (binder != null) {
131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    bound = binder.setViewValue(v, cursor, from[i]);
132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                if (!bound) {
135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    String text = cursor.getString(from[i]);
136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (text == null) {
137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        text = "";
138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    if (v instanceof TextView) {
141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        setViewText((TextView) v, text);
142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    } else if (v instanceof ImageView) {
143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        setViewImage((ImageView) v, text);
144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    } else {
145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                                " view that can be bounds by this SimpleCursorAdapter");
147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                    }
148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                }
149cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            }
150cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Returns the {@link ViewBinder} used to bind data to views.
155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return a ViewBinder or null if the binder does not exist
157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #bindView(android.view.View, android.content.Context, android.database.Cursor)
159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setViewBinder(android.widget.SimpleCursorAdapter.ViewBinder)
160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public ViewBinder getViewBinder() {
162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mViewBinder;
163cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Sets the binder used to bind data to views.
167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param viewBinder the binder used to bind data to views, can be null to
169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *        remove the existing binder
170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #bindView(android.view.View, android.content.Context, android.database.Cursor)
172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getViewBinder()
173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setViewBinder(ViewBinder viewBinder) {
175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mViewBinder = viewBinder;
176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Called by bindView() to set the image for an ImageView but only if
180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * there is no existing ViewBinder or if the existing ViewBinder cannot
181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * handle binding to an ImageView.
182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * By default, the value will be treated as an image resource. If the
184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * value cannot be used as an image resource, the value is used as an
185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * image Uri.
186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Intended to be overridden by Adapters that need to filter strings
188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * retrieved from the database.
189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param v ImageView to receive an image
191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param value the value retrieved from the cursor
192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setViewImage(ImageView v, String value) {
194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        try {
195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            v.setImageResource(Integer.parseInt(value));
196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } catch (NumberFormatException nfe) {
197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            v.setImageURI(Uri.parse(value));
198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Called by bindView() to set the text for a TextView but only if
203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * there is no existing ViewBinder or if the existing ViewBinder cannot
204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * handle binding to an TextView.
205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Intended to be overridden by Adapters that need to filter strings
207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * retrieved from the database.
208cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param v TextView to receive text
210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param text the text to be set for the TextView
211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
212cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setViewText(TextView v, String text) {
213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        v.setText(text);
214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
215cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Return the index of the column used to get a String representation
218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * of the Cursor.
219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return a valid index in the current Cursor or -1
221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setStringConversionColumn(int)
224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter)
225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getCursorToStringConverter()
226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
227cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public int getStringConversionColumn() {
228cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mStringConversionColumn;
229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Defines the index of the column in the Cursor used to get a String
233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * representation of that Cursor. The column is used to convert the
234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Cursor to a String only when the current CursorToStringConverter
235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is null.
236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param stringConversionColumn a valid index in the current Cursor or -1 to use the default
238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *        conversion mechanism
239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getStringConversionColumn()
242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter)
243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getCursorToStringConverter()
244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setStringConversionColumn(int stringConversionColumn) {
246cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mStringConversionColumn = stringConversionColumn;
247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Returns the converter used to convert the filtering Cursor
251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * into a String.
252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return null if the converter does not exist or an instance of
254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *         {@link android.widget.SimpleCursorAdapter.CursorToStringConverter}
255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter)
257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getStringConversionColumn()
258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setStringConversionColumn(int)
259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
260cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public CursorToStringConverter getCursorToStringConverter() {
262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return mCursorToStringConverter;
263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Sets the converter  used to convert the filtering Cursor
267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * into a String.
268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cursorToStringConverter the Cursor to String converter, or
270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *        null to remove the converter
271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setCursorToStringConverter(android.widget.SimpleCursorAdapter.CursorToStringConverter)
273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #getStringConversionColumn()
274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see #setStringConversionColumn(int)
275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
276cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void setCursorToStringConverter(CursorToStringConverter cursorToStringConverter) {
278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mCursorToStringConverter = cursorToStringConverter;
279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Returns a CharSequence representation of the specified Cursor as defined
283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * by the current CursorToStringConverter. If no CursorToStringConverter
284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * has been set, the String conversion column is used instead. If the
285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * conversion column is -1, the returned String is empty if the cursor
286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * is null or Cursor.toString().
287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param cursor the Cursor to convert to a CharSequence
289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @return a non-null CharSequence representing the cursor
291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    @Override
293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public CharSequence convertToString(Cursor cursor) {
294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mCursorToStringConverter != null) {
295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return mCursorToStringConverter.convertToString(cursor);
296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        } else if (mStringConversionColumn > -1) {
297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn            return cursor.getString(mStringConversionColumn);
298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        }
299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return super.convertToString(cursor);
301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Create a map from an array of strings to an array of column-id integers in mCursor.
305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * If mCursor is null, the array will be discarded.
306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param from the Strings naming the columns of interest
308cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
309cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    private void findColumns(String[] from) {
310cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        if (mCursor != 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++) {
317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn                mFrom[i] = mCursor.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) {
326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        Cursor res = super.swapCursor(c);
327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        // rescan columns in case cursor layout is different
328cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        findColumns(mOriginalFrom);
329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        return res;
330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
331cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
332cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
333cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * Change the cursor and change the column-to-view mappings at the same time.
334cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
335cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param c The database cursor.  Can be null if the cursor is not available yet.
336cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param from A list of column names representing the data to bind to the UI.  Can be null
337cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            if the cursor is not available yet.
338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @param to The views that should display column in the "from" parameter.
339cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            These should all be TextViews. The first N views in this list
340cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            are given the values of the first N columns in the from
341cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *            parameter.  Can be null if the cursor is not available yet.
342cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public void changeCursorAndColumns(Cursor c, String[] from, int[] to) {
344cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mOriginalFrom = from;
345cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        mTo = to;
346cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        super.changeCursor(c);
347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        findColumns(mOriginalFrom);
348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This class can be used by external clients of SimpleCursorAdapter
352cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to bind values fom the Cursor to views.
353cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
354cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * You should use this class to bind values from the Cursor to views
355cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * that are not directly supported by SimpleCursorAdapter or to
356cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * change the way binding occurs for views supported by
357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * SimpleCursorAdapter.
358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see SimpleCursorAdapter#bindView(android.view.View, android.content.Context, android.database.Cursor)
360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see SimpleCursorAdapter#setViewImage(ImageView, String)
361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see SimpleCursorAdapter#setViewText(TextView, String)
362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static interface ViewBinder {
364cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Binds the Cursor column defined by the specified index to the specified view.
366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * When binding is handled by this ViewBinder, this method must return true.
368cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * If this method returns false, SimpleCursorAdapter will attempts to handle
369cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * the binding on its own.
370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param view the view to bind the data to
372cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param cursor the cursor to get the data from
373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param columnIndex the column at which the data can be found in the cursor
374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @return true if the data was bound to the view, false otherwise
376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        boolean setViewValue(View view, Cursor cursor, int columnIndex);
378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    /**
381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * This class can be used by external clients of SimpleCursorAdapter
382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * to define how the Cursor should be converted to a String.
383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     *
384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     * @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn     */
386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    public static interface CursorToStringConverter {
387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        /**
388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * Returns a CharSequence representing the specified Cursor.
389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @param cursor the cursor for which a CharSequence representation
391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *        is requested
392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         *
393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         * @return a non-null CharSequence representing the cursor
394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn         */
395cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn        CharSequence convertToString(Cursor cursor);
396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn    }
397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn
398cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn}
399