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 com.example.android.apis.view;
18
19import com.example.android.apis.Shakespeare;
20
21import android.app.ListActivity;
22import android.content.Context;
23import android.os.Bundle;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.BaseAdapter;
27import android.widget.LinearLayout;
28import android.widget.TextView;
29
30
31/**
32 * A list view example where the data comes from a custom ListAdapter
33 */
34public class List4 extends ListActivity {
35
36    @Override
37    public void onCreate(Bundle savedInstanceState) {
38        super.onCreate(savedInstanceState);
39
40        // Use our own list adapter
41        setListAdapter(new SpeechListAdapter(this));
42    }
43
44
45    /**
46     * A sample ListAdapter that presents content from arrays of speeches and
47     * text.
48     *
49     */
50    private class SpeechListAdapter extends BaseAdapter {
51        public SpeechListAdapter(Context context) {
52            mContext = context;
53        }
54
55        /**
56         * The number of items in the list is determined by the number of speeches
57         * in our array.
58         *
59         * @see android.widget.ListAdapter#getCount()
60         */
61        public int getCount() {
62            return Shakespeare.TITLES.length;
63        }
64
65        /**
66         * Since the data comes from an array, just returning the index is
67         * sufficent to get at the data. If we were using a more complex data
68         * structure, we would return whatever object represents one row in the
69         * list.
70         *
71         * @see android.widget.ListAdapter#getItem(int)
72         */
73        public Object getItem(int position) {
74            return position;
75        }
76
77        /**
78         * Use the array index as a unique id.
79         *
80         * @see android.widget.ListAdapter#getItemId(int)
81         */
82        public long getItemId(int position) {
83            return position;
84        }
85
86        /**
87         * Make a SpeechView to hold each row.
88         *
89         * @see android.widget.ListAdapter#getView(int, android.view.View,
90         *      android.view.ViewGroup)
91         */
92        public View getView(int position, View convertView, ViewGroup parent) {
93            SpeechView sv;
94            if (convertView == null) {
95                sv = new SpeechView(mContext, Shakespeare.TITLES[position],
96                        Shakespeare.DIALOGUE[position]);
97            } else {
98                sv = (SpeechView) convertView;
99                sv.setTitle(Shakespeare.TITLES[position]);
100                sv.setDialogue(Shakespeare.DIALOGUE[position]);
101            }
102
103            return sv;
104        }
105
106        /**
107         * Remember our context so we can use it when constructing views.
108         */
109        private Context mContext;
110    }
111
112    /**
113     * We will use a SpeechView to display each speech. It's just a LinearLayout
114     * with two text fields.
115     *
116     */
117    private class SpeechView extends LinearLayout {
118        public SpeechView(Context context, String title, String words) {
119            super(context);
120
121            this.setOrientation(VERTICAL);
122
123            // Here we build the child views in code. They could also have
124            // been specified in an XML file.
125
126            mTitle = new TextView(context);
127            mTitle.setText(title);
128            addView(mTitle, new LinearLayout.LayoutParams(
129                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
130
131            mDialogue = new TextView(context);
132            mDialogue.setText(words);
133            addView(mDialogue, new LinearLayout.LayoutParams(
134                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
135        }
136
137        /**
138         * Convenience method to set the title of a SpeechView
139         */
140        public void setTitle(String title) {
141            mTitle.setText(title);
142        }
143
144        /**
145         * Convenience method to set the dialogue of a SpeechView
146         */
147        public void setDialogue(String words) {
148            mDialogue.setText(words);
149        }
150
151        private TextView mTitle;
152        private TextView mDialogue;
153    }
154}
155