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