1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.widget; 18 19import android.content.Context; 20import android.database.Cursor; 21import android.view.View; 22import android.view.ViewGroup; 23import android.view.LayoutInflater; 24 25 26/** 27 * An easy adapter that creates views defined in an XML file. You can specify 28 * the XML file that defines the appearance of the views. 29 */ 30public abstract class ResourceCursorAdapter extends CursorAdapter { 31 private int mLayout; 32 33 private int mDropDownLayout; 34 35 private LayoutInflater mInflater; 36 37 /** 38 * Constructor the enables auto-requery. 39 * 40 * @deprecated This option is discouraged, as it results in Cursor queries 41 * being performed on the application's UI thread and thus can cause poor 42 * responsiveness or even Application Not Responding errors. As an alternative, 43 * use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}. 44 * 45 * @param context The context where the ListView associated with this adapter is running 46 * @param layout resource identifier of a layout file that defines the views 47 * for this list item. Unless you override them later, this will 48 * define both the item views and the drop down views. 49 */ 50 @Deprecated 51 public ResourceCursorAdapter(Context context, int layout, Cursor c) { 52 super(context, c); 53 mLayout = mDropDownLayout = layout; 54 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 55 } 56 57 /** 58 * Constructor with default behavior as per 59 * {@link CursorAdapter#CursorAdapter(Context, Cursor, boolean)}; it is recommended 60 * you not use this, but instead {@link #ResourceCursorAdapter(Context, int, Cursor, int)}. 61 * When using this constructor, {@link #FLAG_REGISTER_CONTENT_OBSERVER} 62 * will always be set. 63 * 64 * @param context The context where the ListView associated with this adapter is running 65 * @param layout resource identifier of a layout file that defines the views 66 * for this list item. Unless you override them later, this will 67 * define both the item views and the drop down views. 68 * @param c The cursor from which to get the data. 69 * @param autoRequery If true the adapter will call requery() on the 70 * cursor whenever it changes so the most recent 71 * data is always displayed. Using true here is discouraged. 72 */ 73 public ResourceCursorAdapter(Context context, int layout, Cursor c, boolean autoRequery) { 74 super(context, c, autoRequery); 75 mLayout = mDropDownLayout = layout; 76 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 77 } 78 79 /** 80 * Standard constructor. 81 * 82 * @param context The context where the ListView associated with this adapter is running 83 * @param layout Resource identifier of a layout file that defines the views 84 * for this list item. Unless you override them later, this will 85 * define both the item views and the drop down views. 86 * @param c The cursor from which to get the data. 87 * @param flags Flags used to determine the behavior of the adapter, 88 * as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}. 89 */ 90 public ResourceCursorAdapter(Context context, int layout, Cursor c, int flags) { 91 super(context, c, flags); 92 mLayout = mDropDownLayout = layout; 93 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 94 } 95 96 /** 97 * Inflates view(s) from the specified XML file. 98 * 99 * @see android.widget.CursorAdapter#newView(android.content.Context, 100 * android.database.Cursor, ViewGroup) 101 */ 102 @Override 103 public View newView(Context context, Cursor cursor, ViewGroup parent) { 104 return mInflater.inflate(mLayout, parent, false); 105 } 106 107 @Override 108 public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) { 109 return mInflater.inflate(mDropDownLayout, parent, false); 110 } 111 112 /** 113 * <p>Sets the layout resource of the item views.</p> 114 * 115 * @param layout the layout resources used to create item views 116 */ 117 public void setViewResource(int layout) { 118 mLayout = layout; 119 } 120 121 /** 122 * <p>Sets the layout resource of the drop down views.</p> 123 * 124 * @param dropDownLayout the layout resources used to create drop down views 125 */ 126 public void setDropDownViewResource(int dropDownLayout) { 127 mDropDownLayout = dropDownLayout; 128 } 129} 130