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 198e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikasimport static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 208e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas 21cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.content.Context; 22cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.database.Cursor; 23cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.net.Uri; 24c39d9c75590eca86a5e7e32a8824ba04a0d42e9bAlan Viveretteimport android.support.annotation.RestrictTo; 25cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.view.View; 26cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.ImageView; 27cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornimport android.widget.TextView; 28cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 29cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn/** 30cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Static library support version of the framework's {@link android.widget.SimpleCursorAdapter}. 31cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Used to write apps that run on platforms prior to Android 3.0. When running 32cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * on Android 3.0 or above, this implementation is still used; it does not try 33cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to switch to the framework's implementation. See the framework SDK 34cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * documentation for a class overview. 35cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 36cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackbornpublic class SimpleCursorAdapter extends ResourceCursorAdapter { 37cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 38cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * A list of columns containing the data to bind to the UI. 39cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This field should be made private, so it is hidden from the SDK. 40cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@hide} 41cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 428e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas @RestrictTo(LIBRARY_GROUP) 43cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn protected int[] mFrom; 44cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 45cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * A list of View ids representing the views to which the data must be bound. 46cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This field should be made private, so it is hidden from the SDK. 47cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@hide} 48cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 498e10080c914d1ad0784394fa3026b85535535847Aurimas Liutikas @RestrictTo(LIBRARY_GROUP) 50cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn protected int[] mTo; 51cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 52cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private int mStringConversionColumn = -1; 53cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private CursorToStringConverter mCursorToStringConverter; 54cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn private ViewBinder mViewBinder; 55cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 56cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn String[] mOriginalFrom; 57cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 58cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 59cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Constructor the enables auto-requery. 60cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 61cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @deprecated This option is discouraged, as it results in Cursor queries 62cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * being performed on the application's UI thread and thus can cause poor 63cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * responsiveness or even Application Not Responding errors. As an alternative, 64cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}. 65cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 66cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Deprecated 67cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 68cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super(context, layout, c); 69cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mTo = to; 70cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mOriginalFrom = from; 7191af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette findColumns(c, from); 72cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 73cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 74cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 75cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Standard constructor. 764d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas * 77cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param context The context where the ListView associated with this 78cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * SimpleListItemFactory is running 79cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param layout resource identifier of a layout file that defines the views 80cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * for this list item. The layout file should include at least 81cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * those named views defined in "to" 82cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param c The database cursor. Can be null if the cursor is not available yet. 834d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas * @param from A list of column names representing the data to bind to the UI. Can be null 84cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * if the cursor is not available yet. 85cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param to The views that should display column in the "from" parameter. 86cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * These should all be TextViews. The first N views in this list 87cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * are given the values of the first N columns in the from 88cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * parameter. Can be null if the cursor is not available yet. 89cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param flags Flags used to determine the behavior of the adapter, 90cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}. 91cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 92cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, 93cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int[] to, int flags) { 94cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn super(context, layout, c, flags); 95cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mTo = to; 96cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mOriginalFrom = from; 9791af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette findColumns(c, from); 98cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 99cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 100cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 101cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Binds all of the field names passed into the "to" parameter of the 102cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * constructor with their corresponding cursor columns as specified in the 103cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * "from" parameter. 104cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 105cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Binding occurs in two phases. First, if a 106cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link android.widget.SimpleCursorAdapter.ViewBinder} is available, 107cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link ViewBinder#setViewValue(android.view.View, android.database.Cursor, int)} 108cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * is invoked. If the returned value is true, binding has occured. If the 109cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * returned value is false and the view to bind is a TextView, 110cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #setViewText(TextView, String)} is invoked. If the returned value is 111cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * false and the view to bind is an ImageView, 112cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link #setViewImage(ImageView, String)} is invoked. If no appropriate 113cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * binding can be found, an {@link IllegalStateException} is thrown. 114cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 115cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @throws IllegalStateException if binding cannot occur 1164d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas * 11791af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * @see android.widget.CursorAdapter#bindView(View, Context, Cursor) 118cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #getViewBinder() 11991af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * @see #setViewBinder(ViewBinder) 120cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #setViewImage(ImageView, String) 121cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #setViewText(TextView, String) 122cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 123cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 124cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void bindView(View view, Context context, Cursor cursor) { 125cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final ViewBinder binder = mViewBinder; 126cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final int count = mTo.length; 127cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final int[] from = mFrom; 128cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final int[] to = mTo; 129cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 130cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (int i = 0; i < count; i++) { 131cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn final View v = view.findViewById(to[i]); 132cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (v != null) { 133cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean bound = false; 134cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (binder != null) { 135cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn bound = binder.setViewValue(v, cursor, from[i]); 136cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 137cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 138cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (!bound) { 139cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn String text = cursor.getString(from[i]); 140cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (text == null) { 141cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn text = ""; 142cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 143cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 144cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (v instanceof TextView) { 145cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setViewText((TextView) v, text); 146cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else if (v instanceof ImageView) { 147cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn setViewImage((ImageView) v, text); 148cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 1494d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas throw new IllegalStateException(v.getClass().getName() + " is not a " 1504d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas + " view that can be bounds by this SimpleCursorAdapter"); 151cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 152cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 153cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 154cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 155cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 156cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 157cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 158cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Returns the {@link ViewBinder} used to bind data to views. 159cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 160cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return a ViewBinder or null if the binder does not exist 161cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 162cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #bindView(android.view.View, android.content.Context, android.database.Cursor) 16391af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * @see #setViewBinder(ViewBinder) 164cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 165cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public ViewBinder getViewBinder() { 166cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mViewBinder; 167cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 168cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 169cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 170cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Sets the binder used to bind data to views. 171cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 172cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param viewBinder the binder used to bind data to views, can be null to 173cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * remove the existing binder 174cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 175cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #bindView(android.view.View, android.content.Context, android.database.Cursor) 176cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #getViewBinder() 177cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 178cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setViewBinder(ViewBinder viewBinder) { 179cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mViewBinder = viewBinder; 180cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 181cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 182cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 183cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Called by bindView() to set the image for an ImageView but only if 184cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * there is no existing ViewBinder or if the existing ViewBinder cannot 185cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * handle binding to an ImageView. 186cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 187cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * By default, the value will be treated as an image resource. If the 188cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * value cannot be used as an image resource, the value is used as an 189cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * image Uri. 190cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 191cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Intended to be overridden by Adapters that need to filter strings 192cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * retrieved from the database. 193cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 194cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param v ImageView to receive an image 195cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param value the value retrieved from the cursor 196cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 197cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setViewImage(ImageView v, String value) { 198cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn try { 199cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn v.setImageResource(Integer.parseInt(value)); 200cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } catch (NumberFormatException nfe) { 201cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn v.setImageURI(Uri.parse(value)); 202cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 203cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 204cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 205cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 206cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Called by bindView() to set the text for a TextView but only if 207cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * there is no existing ViewBinder or if the existing ViewBinder cannot 20891af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * handle binding to a TextView. 209cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 210cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Intended to be overridden by Adapters that need to filter strings 211cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * retrieved from the database. 2124d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas * 213cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param v TextView to receive text 214cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param text the text to be set for the TextView 2154d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas */ 216cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setViewText(TextView v, String text) { 217cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn v.setText(text); 218cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 219cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 220cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 221cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Return the index of the column used to get a String representation 222cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * of the Cursor. 223cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 224cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return a valid index in the current Cursor or -1 225cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 226cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see android.widget.CursorAdapter#convertToString(android.database.Cursor) 2274d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas * @see #setStringConversionColumn(int) 22891af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * @see #setCursorToStringConverter(CursorToStringConverter) 229cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #getCursorToStringConverter() 230cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 231cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public int getStringConversionColumn() { 232cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mStringConversionColumn; 233cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 234cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 235cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 236cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Defines the index of the column in the Cursor used to get a String 237cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * representation of that Cursor. The column is used to convert the 238cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Cursor to a String only when the current CursorToStringConverter 239cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * is null. 240cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 241cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param stringConversionColumn a valid index in the current Cursor or -1 to use the default 242cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * conversion mechanism 243cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 244cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see android.widget.CursorAdapter#convertToString(android.database.Cursor) 245cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #getStringConversionColumn() 24691af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * @see #setCursorToStringConverter(CursorToStringConverter) 247cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #getCursorToStringConverter() 248cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 249cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setStringConversionColumn(int stringConversionColumn) { 250cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mStringConversionColumn = stringConversionColumn; 251cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 252cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 253cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 254cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Returns the converter used to convert the filtering Cursor 255cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * into a String. 256cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 257cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return null if the converter does not exist or an instance of 258cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * {@link android.widget.SimpleCursorAdapter.CursorToStringConverter} 259cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 26091af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * @see #setCursorToStringConverter(CursorToStringConverter) 261cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #getStringConversionColumn() 262cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #setStringConversionColumn(int) 263cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see android.widget.CursorAdapter#convertToString(android.database.Cursor) 264cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 265cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public CursorToStringConverter getCursorToStringConverter() { 266cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mCursorToStringConverter; 267cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 268cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 269cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 270cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Sets the converter used to convert the filtering Cursor 271cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * into a String. 272cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 273cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param cursorToStringConverter the Cursor to String converter, or 274cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * null to remove the converter 275cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 27691af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * @see #setCursorToStringConverter(CursorToStringConverter) 277cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #getStringConversionColumn() 278cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see #setStringConversionColumn(int) 279cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see android.widget.CursorAdapter#convertToString(android.database.Cursor) 280cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 281cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void setCursorToStringConverter(CursorToStringConverter cursorToStringConverter) { 282cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mCursorToStringConverter = cursorToStringConverter; 283cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 284cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 285cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 286cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Returns a CharSequence representation of the specified Cursor as defined 287cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * by the current CursorToStringConverter. If no CursorToStringConverter 288cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * has been set, the String conversion column is used instead. If the 289cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * conversion column is -1, the returned String is empty if the cursor 290cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * is null or Cursor.toString(). 291cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 292cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param cursor the Cursor to convert to a CharSequence 293cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 294cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return a non-null CharSequence representing the cursor 295cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 296cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 297cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public CharSequence convertToString(Cursor cursor) { 298cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mCursorToStringConverter != null) { 299cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return mCursorToStringConverter.convertToString(cursor); 300cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else if (mStringConversionColumn > -1) { 301cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return cursor.getString(mStringConversionColumn); 302cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 303cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 304cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn return super.convertToString(cursor); 305cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 306cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 307cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 30891af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * Create a map from an array of strings to an array of column-id integers in cursor c. 30991af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * If c is null, the array will be discarded. 31091af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * 31191af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * @param c the cursor to find the columns from 312cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param from the Strings naming the columns of interest 313cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 31491af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette private void findColumns(Cursor c, String[] from) { 31591af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette if (c != null) { 316cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int i; 317cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn int count = from.length; 318cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn if (mFrom == null || mFrom.length != count) { 319cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFrom = new int[count]; 320cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 321cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn for (i = 0; i < count; i++) { 32291af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette mFrom[i] = c.getColumnIndexOrThrow(from[i]); 323cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 324cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } else { 325cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mFrom = null; 326cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 327cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 328cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 329cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn @Override 330cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public Cursor swapCursor(Cursor c) { 33191af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette // super.swapCursor() will notify observers before we have 33291af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette // a valid mapping, make sure we have a mapping before this 33391af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette // happens 33491af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette findColumns(c, mOriginalFrom); 33591af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette return super.swapCursor(c); 336cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 33791af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette 338cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 339cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Change the cursor and change the column-to-view mappings at the same time. 34091af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * 341cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param c The database cursor. Can be null if the cursor is not available yet. 3424d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas * @param from A list of column names representing the data to bind to the UI. Can be null 343cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * if the cursor is not available yet. 344cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param to The views that should display column in the "from" parameter. 345cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * These should all be TextViews. The first N views in this list 346cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * are given the values of the first N columns in the from 347cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * parameter. Can be null if the cursor is not available yet. 348cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 349cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn public void changeCursorAndColumns(Cursor c, String[] from, int[] to) { 350cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mOriginalFrom = from; 351cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn mTo = to; 35291af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette // super.changeCursor() will notify observers before we have 35391af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette // a valid mapping, make sure we have a mapping before this 35491af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette // happens 35591af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette findColumns(c, mOriginalFrom); 35691af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette super.changeCursor(c); 357cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 358cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 359cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 360cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This class can be used by external clients of SimpleCursorAdapter 361cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to bind values fom the Cursor to views. 362cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 363cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * You should use this class to bind values from the Cursor to views 364cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * that are not directly supported by SimpleCursorAdapter or to 365cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * change the way binding occurs for views supported by 366cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * SimpleCursorAdapter. 367cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 36891af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette * @see SimpleCursorAdapter#bindView(View, Context, Cursor) 3694d2c7b7c4f194034c5f17c4bee7320d808aabe4cAurimas Liutikas * @see SimpleCursorAdapter#setViewImage(ImageView, String) 370cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see SimpleCursorAdapter#setViewText(TextView, String) 371cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 37291af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette public interface ViewBinder { 373cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 374cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Binds the Cursor column defined by the specified index to the specified view. 375cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 376cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * When binding is handled by this ViewBinder, this method must return true. 377cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * If this method returns false, SimpleCursorAdapter will attempts to handle 378cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * the binding on its own. 379cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 380cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param view the view to bind the data to 381cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param cursor the cursor to get the data from 382cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param columnIndex the column at which the data can be found in the cursor 383cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 384cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return true if the data was bound to the view, false otherwise 385cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 386cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn boolean setViewValue(View view, Cursor cursor, int columnIndex); 387cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 388cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 389cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 390cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * This class can be used by external clients of SimpleCursorAdapter 391cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * to define how the Cursor should be converted to a String. 392cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 393cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @see android.widget.CursorAdapter#convertToString(android.database.Cursor) 394cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 39591af1489f851fe8c4b015b519e2ce215189878b1Alan Viverette public interface CursorToStringConverter { 396cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn /** 397cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * Returns a CharSequence representing the specified Cursor. 398cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 399cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @param cursor the cursor for which a CharSequence representation 400cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * is requested 401cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * 402cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn * @return a non-null CharSequence representing the cursor 403cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn */ 404cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn CharSequence convertToString(Cursor cursor); 405cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn } 406cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn 407cba2e2c881e8e16ea5025b564c94320174d65f01Dianne Hackborn} 408