1/*
2 * Copyright (C) 2006 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 * A fairly simple ExpandableListAdapter that creates views defined in an XML
27 * file. You can specify the XML file that defines the appearance of the views.
28 */
29public abstract class ResourceCursorTreeAdapter extends CursorTreeAdapter {
30    private int mCollapsedGroupLayout;
31    private int mExpandedGroupLayout;
32    private int mChildLayout;
33    private int mLastChildLayout;
34    private LayoutInflater mInflater;
35
36    /**
37     * Constructor.
38     *
39     * @param context The context where the ListView associated with this
40     *            SimpleListItemFactory is running
41     * @param cursor The database cursor
42     * @param collapsedGroupLayout resource identifier of a layout file that
43     *            defines the views for collapsed groups.
44     * @param expandedGroupLayout resource identifier of a layout file that
45     *            defines the views for expanded groups.
46     * @param childLayout resource identifier of a layout file that defines the
47     *            views for all children but the last..
48     * @param lastChildLayout resource identifier of a layout file that defines
49     *            the views for the last child of a group.
50     */
51    public ResourceCursorTreeAdapter(Context context, Cursor cursor, int collapsedGroupLayout,
52            int expandedGroupLayout, int childLayout, int lastChildLayout) {
53        super(cursor, context);
54
55        mCollapsedGroupLayout = collapsedGroupLayout;
56        mExpandedGroupLayout = expandedGroupLayout;
57        mChildLayout = childLayout;
58        mLastChildLayout = lastChildLayout;
59
60        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
61    }
62
63    /**
64     * Constructor.
65     *
66     * @param context The context where the ListView associated with this
67     *            SimpleListItemFactory is running
68     * @param cursor The database cursor
69     * @param collapsedGroupLayout resource identifier of a layout file that
70     *            defines the views for collapsed groups.
71     * @param expandedGroupLayout resource identifier of a layout file that
72     *            defines the views for expanded groups.
73     * @param childLayout resource identifier of a layout file that defines the
74     *            views for all children.
75     */
76    public ResourceCursorTreeAdapter(Context context, Cursor cursor, int collapsedGroupLayout,
77            int expandedGroupLayout, int childLayout) {
78        this(context, cursor, collapsedGroupLayout, expandedGroupLayout, childLayout, childLayout);
79    }
80
81    /**
82     * Constructor.
83     *
84     * @param context The context where the ListView associated with this
85     *            SimpleListItemFactory is running
86     * @param cursor The database cursor
87     * @param groupLayout resource identifier of a layout file that defines the
88     *            views for all groups.
89     * @param childLayout resource identifier of a layout file that defines the
90     *            views for all children.
91     */
92    public ResourceCursorTreeAdapter(Context context, Cursor cursor, int groupLayout,
93            int childLayout) {
94        this(context, cursor, groupLayout, groupLayout, childLayout, childLayout);
95    }
96
97    @Override
98    public View newChildView(Context context, Cursor cursor, boolean isLastChild,
99            ViewGroup parent) {
100        return mInflater.inflate((isLastChild) ? mLastChildLayout : mChildLayout, parent, false);
101    }
102
103    @Override
104    public View newGroupView(Context context, Cursor cursor, boolean isExpanded, ViewGroup parent) {
105        return mInflater.inflate((isExpanded) ? mExpandedGroupLayout : mCollapsedGroupLayout,
106                parent, false);
107    }
108
109}
110