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     * @deprecated This option is discouraged, as it results in Cursor queries
67     * being performed on the application's UI thread and thus can cause poor
68     * responsiveness or even Application Not Responding errors.  As an alternative,
69     * use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}.
70     *
71     * @param context The context where the ListView associated with this adapter is running
72     * @param layout resource identifier of a layout file that defines the views
73     *            for this list item.  Unless you override them later, this will
74     *            define both the item views and the drop down views.
75     * @param c The cursor from which to get the data.
76     * @param autoRequery If true the adapter will call requery() on the
77     *                    cursor whenever it changes so the most recent
78     *                    data is always displayed.  Using true here is discouraged.
79     */
80    @Deprecated
81    public ResourceCursorAdapter(Context context, int layout, Cursor c, boolean autoRequery) {
82        super(context, c, autoRequery);
83        mLayout = mDropDownLayout = layout;
84        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
85    }
86
87    /**
88     * Standard constructor.
89     *
90     * @param context The context where the ListView associated with this adapter is running
91     * @param layout Resource identifier of a layout file that defines the views
92     *            for this list item.  Unless you override them later, this will
93     *            define both the item views and the drop down views.
94     * @param c The cursor from which to get the data.
95     * @param flags Flags used to determine the behavior of the adapter,
96     * as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}.
97     */
98    public ResourceCursorAdapter(Context context, int layout, Cursor c, int flags) {
99        super(context, c, flags);
100        mLayout = mDropDownLayout = layout;
101        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
102    }
103
104    /**
105     * Inflates view(s) from the specified XML file.
106     *
107     * @see android.widget.CursorAdapter#newView(android.content.Context,
108     *      android.database.Cursor, ViewGroup)
109     */
110    @Override
111    public View newView(Context context, Cursor cursor, ViewGroup parent) {
112        return mInflater.inflate(mLayout, parent, false);
113    }
114
115    @Override
116    public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
117        return mInflater.inflate(mDropDownLayout, parent, false);
118    }
119
120    /**
121     * <p>Sets the layout resource of the item views.</p>
122     *
123     * @param layout the layout resources used to create item views
124     */
125    public void setViewResource(int layout) {
126        mLayout = layout;
127    }
128
129    /**
130     * <p>Sets the layout resource of the drop down views.</p>
131     *
132     * @param dropDownLayout the layout resources used to create drop down views
133     */
134    public void setDropDownViewResource(int dropDownLayout) {
135        mDropDownLayout = dropDownLayout;
136    }
137}
138