The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.widget; |
| 18 | |
| 19 | import android.content.Context; |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 20 | import android.content.res.Resources; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.database.Cursor; |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 22 | import android.view.ContextThemeWrapper; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 23 | import android.view.LayoutInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.view.View; |
| 25 | import android.view.ViewGroup; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | |
| 28 | /** |
| 29 | * An easy adapter that creates views defined in an XML file. You can specify |
| 30 | * the XML file that defines the appearance of the views. |
| 31 | */ |
| 32 | public abstract class ResourceCursorAdapter extends CursorAdapter { |
| 33 | private int mLayout; |
| 34 | |
| 35 | private int mDropDownLayout; |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 36 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | private LayoutInflater mInflater; |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 38 | private LayoutInflater mDropDownInflater; |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | /** |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 41 | * Constructor the enables auto-requery. |
| 42 | * |
| 43 | * @deprecated This option is discouraged, as it results in Cursor queries |
| 44 | * being performed on the application's UI thread and thus can cause poor |
| 45 | * responsiveness or even Application Not Responding errors. As an alternative, |
| 46 | * use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}. |
Jeff Hamilton | b721b47 | 2010-08-10 16:02:06 -0500 | [diff] [blame] | 47 | * |
| 48 | * @param context The context where the ListView associated with this adapter is running |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | * @param layout resource identifier of a layout file that defines the views |
| 50 | * for this list item. Unless you override them later, this will |
| 51 | * define both the item views and the drop down views. |
| 52 | */ |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 53 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | public ResourceCursorAdapter(Context context, int layout, Cursor c) { |
| 55 | super(context, c); |
| 56 | mLayout = mDropDownLayout = layout; |
| 57 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 58 | mDropDownInflater = mInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | } |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | /** |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 62 | * Constructor with default behavior as per |
| 63 | * {@link CursorAdapter#CursorAdapter(Context, Cursor, boolean)}; it is recommended |
| 64 | * you not use this, but instead {@link #ResourceCursorAdapter(Context, int, Cursor, int)}. |
| 65 | * When using this constructor, {@link #FLAG_REGISTER_CONTENT_OBSERVER} |
| 66 | * will always be set. |
Jeff Hamilton | b721b47 | 2010-08-10 16:02:06 -0500 | [diff] [blame] | 67 | * |
| 68 | * @param context The context where the ListView associated with this adapter is running |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | * @param layout resource identifier of a layout file that defines the views |
| 70 | * for this list item. Unless you override them later, this will |
| 71 | * define both the item views and the drop down views. |
| 72 | * @param c The cursor from which to get the data. |
| 73 | * @param autoRequery If true the adapter will call requery() on the |
| 74 | * cursor whenever it changes so the most recent |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 75 | * data is always displayed. Using true here is discouraged. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | */ |
| 77 | public ResourceCursorAdapter(Context context, int layout, Cursor c, boolean autoRequery) { |
| 78 | super(context, c, autoRequery); |
| 79 | mLayout = mDropDownLayout = layout; |
| 80 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 81 | mDropDownInflater = mInflater; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 85 | * Standard constructor. |
Jeff Hamilton | b721b47 | 2010-08-10 16:02:06 -0500 | [diff] [blame] | 86 | * |
| 87 | * @param context The context where the ListView associated with this adapter is running |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 88 | * @param layout Resource identifier of a layout file that defines the views |
Jeff Hamilton | b721b47 | 2010-08-10 16:02:06 -0500 | [diff] [blame] | 89 | * for this list item. Unless you override them later, this will |
| 90 | * define both the item views and the drop down views. |
| 91 | * @param c The cursor from which to get the data. |
Dianne Hackborn | c918935 | 2010-12-15 14:57:25 -0800 | [diff] [blame] | 92 | * @param flags Flags used to determine the behavior of the adapter, |
| 93 | * as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}. |
Jeff Hamilton | b721b47 | 2010-08-10 16:02:06 -0500 | [diff] [blame] | 94 | */ |
| 95 | public ResourceCursorAdapter(Context context, int layout, Cursor c, int flags) { |
| 96 | super(context, c, flags); |
| 97 | mLayout = mDropDownLayout = layout; |
| 98 | mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 99 | mDropDownInflater = mInflater; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Sets the {@link android.content.res.Resources.Theme} against which drop-down views are |
| 104 | * inflated. |
| 105 | * <p> |
| 106 | * By default, drop-down views are inflated against the theme of the |
| 107 | * {@link Context} passed to the adapter's constructor. |
| 108 | * |
| 109 | * @param theme the theme against which to inflate drop-down views or |
| 110 | * {@code null} to use the theme from the adapter's context |
| 111 | * @see #newDropDownView(Context, Cursor, ViewGroup) |
| 112 | */ |
| 113 | @Override |
| 114 | public void setDropDownViewTheme(Resources.Theme theme) { |
| 115 | super.setDropDownViewTheme(theme); |
| 116 | |
| 117 | if (theme == null) { |
| 118 | mDropDownInflater = null; |
| 119 | } else if (theme == mInflater.getContext().getTheme()) { |
| 120 | mDropDownInflater = mInflater; |
| 121 | } else { |
| 122 | final Context context = new ContextThemeWrapper(mContext, theme); |
| 123 | mDropDownInflater = LayoutInflater.from(context); |
| 124 | } |
Jeff Hamilton | b721b47 | 2010-08-10 16:02:06 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | * Inflates view(s) from the specified XML file. |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 129 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | * @see android.widget.CursorAdapter#newView(android.content.Context, |
| 131 | * android.database.Cursor, ViewGroup) |
| 132 | */ |
| 133 | @Override |
| 134 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 135 | return mInflater.inflate(mLayout, parent, false); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) { |
Alan Viverette | b9ead4a | 2015-01-14 10:43:31 -0800 | [diff] [blame] | 140 | return mDropDownInflater.inflate(mDropDownLayout, parent, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * <p>Sets the layout resource of the item views.</p> |
| 145 | * |
| 146 | * @param layout the layout resources used to create item views |
| 147 | */ |
| 148 | public void setViewResource(int layout) { |
| 149 | mLayout = layout; |
| 150 | } |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | /** |
| 153 | * <p>Sets the layout resource of the drop down views.</p> |
| 154 | * |
| 155 | * @param dropDownLayout the layout resources used to create drop down views |
| 156 | */ |
| 157 | public void setDropDownViewResource(int dropDownLayout) { |
| 158 | mDropDownLayout = dropDownLayout; |
| 159 | } |
| 160 | } |