blob: 9732bb105c3449f66f7aaaf7e4a05e46229279f3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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;
Alan Viveretteb9ead4a2015-01-14 10:43:31 -080020import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.database.Cursor;
Alan Viveretteb9ead4a2015-01-14 10:43:31 -080022import android.view.ContextThemeWrapper;
Aurimas Liutikas99441c52016-10-11 16:48:32 -070023import android.view.LayoutInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.view.View;
25import android.view.ViewGroup;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
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 */
32public abstract class ResourceCursorAdapter extends CursorAdapter {
33 private int mLayout;
34
35 private int mDropDownLayout;
Alan Viveretteb9ead4a2015-01-14 10:43:31 -080036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 private LayoutInflater mInflater;
Alan Viveretteb9ead4a2015-01-14 10:43:31 -080038 private LayoutInflater mDropDownInflater;
39
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 /**
Dianne Hackbornc9189352010-12-15 14:57:25 -080041 * 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 Hamiltonb721b472010-08-10 16:02:06 -050047 *
48 * @param context The context where the ListView associated with this adapter is running
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * @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 Hackbornc9189352010-12-15 14:57:25 -080053 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 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 Viveretteb9ead4a2015-01-14 10:43:31 -080058 mDropDownInflater = mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 }
Alan Viveretteb9ead4a2015-01-14 10:43:31 -080060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 /**
Dianne Hackbornc9189352010-12-15 14:57:25 -080062 * 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 Hamiltonb721b472010-08-10 16:02:06 -050067 *
68 * @param context The context where the ListView associated with this adapter is running
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 * @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 Hackbornc9189352010-12-15 14:57:25 -080075 * data is always displayed. Using true here is discouraged.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 */
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 Viveretteb9ead4a2015-01-14 10:43:31 -080081 mDropDownInflater = mInflater;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 }
83
84 /**
Dianne Hackbornc9189352010-12-15 14:57:25 -080085 * Standard constructor.
Jeff Hamiltonb721b472010-08-10 16:02:06 -050086 *
87 * @param context The context where the ListView associated with this adapter is running
Dianne Hackbornc9189352010-12-15 14:57:25 -080088 * @param layout Resource identifier of a layout file that defines the views
Jeff Hamiltonb721b472010-08-10 16:02:06 -050089 * 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 Hackbornc9189352010-12-15 14:57:25 -080092 * @param flags Flags used to determine the behavior of the adapter,
93 * as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}.
Jeff Hamiltonb721b472010-08-10 16:02:06 -050094 */
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 Viveretteb9ead4a2015-01-14 10:43:31 -080099 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 Hamiltonb721b472010-08-10 16:02:06 -0500125 }
126
127 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 * Inflates view(s) from the specified XML file.
Alan Viveretteb9ead4a2015-01-14 10:43:31 -0800129 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * @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 Viveretteb9ead4a2015-01-14 10:43:31 -0800140 return mDropDownInflater.inflate(mDropDownLayout, parent, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 }
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 Liutikas99441c52016-10-11 16:48:32 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 /**
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}