ResourceCursorAdapter.java revision 935ae463d495d41155e27feb849768ad2b8b16db
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
17package android.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/**
27 * An easy adapter that creates views defined in an XML file. You can specify
28 * the XML file that defines the appearance of the views.
29 */
30public abstract class ResourceCursorAdapter extends CursorAdapter {
31    private int mLayout;
32
33    private int mDropDownLayout;
34
35    private LayoutInflater mInflater;
36
37    /**
38     * Constructor.
39     *
40     * @param context The context where the ListView associated with this
41     *            SimpleListItemFactory is running
42     * @param layout resource identifier of a layout file that defines the views
43     *            for this list item.  Unless you override them later, this will
44     *            define both the item views and the drop down views.
45     */
46    public ResourceCursorAdapter(Context context, int layout, Cursor c) {
47        super(context, c);
48        mLayout = mDropDownLayout = layout;
49        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
50    }
51
52    /**
53     * Constructor.
54     *
55     * @param context The context where the ListView associated with this
56     *            SimpleListItemFactory is running
57     * @param layout resource identifier of a layout file that defines the views
58     *            for this list item.  Unless you override them later, this will
59     *            define both the item views and the drop down views.
60     * @param c The cursor from which to get the data.
61     * @param autoRequery If true the adapter will call requery() on the
62     *                    cursor whenever it changes so the most recent
63     *                    data is always displayed.
64     */
65    public ResourceCursorAdapter(Context context, int layout, Cursor c, boolean autoRequery) {
66        super(context, c, autoRequery);
67        mLayout = mDropDownLayout = layout;
68        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
69    }
70
71    /**
72     * Inflates view(s) from the specified XML file.
73     *
74     * @see android.widget.CursorAdapter#newView(android.content.Context,
75     *      android.database.Cursor, ViewGroup)
76     */
77    @Override
78    public View newView(Context context, Cursor cursor, ViewGroup parent) {
79        return mInflater.inflate(mLayout, parent, false);
80    }
81
82    @Override
83    public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
84        return mInflater.inflate(mDropDownLayout, parent, false);
85    }
86
87    /**
88     * <p>Sets the layout resource of the item views.</p>
89     *
90     * @param layout the layout resources used to create item views
91     */
92    public void setViewResource(int layout) {
93        mLayout = layout;
94    }
95
96    /**
97     * <p>Sets the layout resource of the drop down views.</p>
98     *
99     * @param dropDownLayout the layout resources used to create drop down views
100     */
101    public void setDropDownViewResource(int dropDownLayout) {
102        mDropDownLayout = dropDownLayout;
103    }
104}
105