ResourceCursorAdapter.java revision 076357b8567458d4b6dfdcf839ef751634cd2bfb
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     * Inflates view(s) from the specified XML file.
54     *
55     * @see android.widget.CursorAdapter#newView(android.content.Context,
56     *      android.database.Cursor, ViewGroup)
57     */
58    @Override
59    public View newView(Context context, Cursor cursor, ViewGroup parent) {
60        return mInflater.inflate(mLayout, parent, false);
61    }
62
63    @Override
64    public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
65        return mInflater.inflate(mDropDownLayout, parent, false);
66    }
67
68    /**
69     * <p>Sets the layout resource of the item views.</p>
70     *
71     * @param layout the layout resources used to create item views
72     */
73    public void setViewResource(int layout) {
74        mLayout = layout;
75    }
76
77    /**
78     * <p>Sets the layout resource of the drop down views.</p>
79     *
80     * @param dropDownLayout the layout resources used to create drop down views
81     */
82    public void setDropDownViewResource(int dropDownLayout) {
83        mDropDownLayout = dropDownLayout;
84    }
85}
86