1/*
2 * Copyright (C) 2011 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.support.v4.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 * Static library support version of the framework's {@link android.widget.ResourceCursorAdapter}.
27 * Used to write apps that run on platforms prior to Android 3.0.  When running
28 * on Android 3.0 or above, this implementation is still used; it does not try
29 * to switch to the framework's implementation.  See the framework SDK
30 * documentation for a class overview.
31 */
32public abstract class ResourceCursorAdapter extends CursorAdapter {
33    private int mLayout;
34
35    private int mDropDownLayout;
36
37    private LayoutInflater mInflater;
38
39    /**
40     * Constructor the enables auto-requery.
41     *
42     * @deprecated This option is discouraged, as it results in Cursor queries
43     * being performed on the application's UI thread and thus can cause poor
44     * responsiveness or even Application Not Responding errors.  As an alternative,
45     * use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}.
46     *
47     * @param context The context where the ListView associated with this adapter is running
48     * @param layout resource identifier of a layout file that defines the views
49     *            for this list item.  Unless you override them later, this will
50     *            define both the item views and the drop down views.
51     */
52    @Deprecated
53    public ResourceCursorAdapter(Context context, int layout, Cursor c) {
54        super(context, c);
55        mLayout = mDropDownLayout = layout;
56        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
57    }
58
59    /**
60     * Constructor with default behavior as per
61     * {@link CursorAdapter#CursorAdapter(Context, Cursor, boolean)}; it is recommended
62     * you not use this, but instead {@link #ResourceCursorAdapter(Context, int, Cursor, int)}.
63     * When using this constructor, {@link #FLAG_REGISTER_CONTENT_OBSERVER}
64     * will always be set.
65     *
66     * @param context The context where the ListView associated with this adapter is running
67     * @param layout resource identifier of a layout file that defines the views
68     *            for this list item.  Unless you override them later, this will
69     *            define both the item views and the drop down views.
70     * @param c The cursor from which to get the data.
71     * @param autoRequery If true the adapter will call requery() on the
72     *                    cursor whenever it changes so the most recent
73     *                    data is always displayed.  Using true here is discouraged.
74     */
75    public ResourceCursorAdapter(Context context, int layout, Cursor c, boolean autoRequery) {
76        super(context, c, autoRequery);
77        mLayout = mDropDownLayout = layout;
78        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
79    }
80
81    /**
82     * Standard constructor.
83     *
84     * @param context The context where the ListView associated with this adapter is running
85     * @param layout Resource identifier of a layout file that defines the views
86     *            for this list item.  Unless you override them later, this will
87     *            define both the item views and the drop down views.
88     * @param c The cursor from which to get the data.
89     * @param flags Flags used to determine the behavior of the adapter,
90     * as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}.
91     */
92    public ResourceCursorAdapter(Context context, int layout, Cursor c, int flags) {
93        super(context, c, flags);
94        mLayout = mDropDownLayout = layout;
95        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
96    }
97
98    /**
99     * Inflates view(s) from the specified XML file.
100     *
101     * @see android.widget.CursorAdapter#newView(android.content.Context,
102     *      android.database.Cursor, ViewGroup)
103     */
104    @Override
105    public View newView(Context context, Cursor cursor, ViewGroup parent) {
106        return mInflater.inflate(mLayout, parent, false);
107    }
108
109    @Override
110    public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
111        return mInflater.inflate(mDropDownLayout, parent, false);
112    }
113
114    /**
115     * <p>Sets the layout resource of the item views.</p>
116     *
117     * @param layout the layout resources used to create item views
118     */
119    public void setViewResource(int layout) {
120        mLayout = layout;
121    }
122
123    /**
124     * <p>Sets the layout resource of the drop down views.</p>
125     *
126     * @param dropDownLayout the layout resources used to create drop down views
127     */
128    public void setDropDownViewResource(int dropDownLayout) {
129        mDropDownLayout = dropDownLayout;
130    }
131}
132