• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.example.android.apis.view;
18 
19 //Need the following import to get access to the app resources, since this
20 //class is in a sub-package.
21 import android.app.ListActivity;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.BaseAdapter;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
29 
30 
31 /**
32  * A list view example where the data comes from a custom ListAdapter
33  */
34 public class List4 extends ListActivity {
35 
36     @Override
onCreate(Bundle savedInstanceState)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 {
SpeechListAdapter(Context context)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          */
getCount()61         public int getCount() {
62             return mTitles.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          */
getItem(int position)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          */
getItemId(int position)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          */
getView(int position, View convertView, ViewGroup parent)92         public View getView(int position, View convertView, ViewGroup parent) {
93             SpeechView sv;
94             if (convertView == null) {
95                 sv = new SpeechView(mContext, mTitles[position],
96                         mDialogue[position]);
97             } else {
98                 sv = (SpeechView) convertView;
99                 sv.setTitle(mTitles[position]);
100                 sv.setDialogue(mDialogue[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          * Our data, part 1.
113          */
114         private String[] mTitles =
115         {
116                 "Henry IV (1)",
117                 "Henry V",
118                 "Henry VIII",
119                 "Richard II",
120                 "Richard III",
121                 "Merchant of Venice",
122                 "Othello",
123                 "King Lear"
124         };
125 
126         /**
127          * Our data, part 2.
128          */
129         private String[] mDialogue =
130         {
131                 "So shaken as we are, so wan with care," +
132                 "Find we a time for frighted peace to pant," +
133                 "And breathe short-winded accents of new broils" +
134                 "To be commenced in strands afar remote." +
135                 "No more the thirsty entrance of this soil" +
136                 "Shall daub her lips with her own children's blood;" +
137                 "Nor more shall trenching war channel her fields," +
138                 "Nor bruise her flowerets with the armed hoofs" +
139                 "Of hostile paces: those opposed eyes," +
140                 "Which, like the meteors of a troubled heaven," +
141                 "All of one nature, of one substance bred," +
142                 "Did lately meet in the intestine shock" +
143                 "And furious close of civil butchery" +
144                 "Shall now, in mutual well-beseeming ranks," +
145                 "March all one way and be no more opposed" +
146                 "Against acquaintance, kindred and allies:" +
147                 "The edge of war, like an ill-sheathed knife," +
148                 "No more shall cut his master. Therefore, friends," +
149                 "As far as to the sepulchre of Christ," +
150                 "Whose soldier now, under whose blessed cross" +
151                 "We are impressed and engaged to fight," +
152                 "Forthwith a power of English shall we levy;" +
153                 "Whose arms were moulded in their mothers' womb" +
154                 "To chase these pagans in those holy fields" +
155                 "Over whose acres walk'd those blessed feet" +
156                 "Which fourteen hundred years ago were nail'd" +
157                 "For our advantage on the bitter cross." +
158                 "But this our purpose now is twelve month old," +
159                 "And bootless 'tis to tell you we will go:" +
160                 "Therefore we meet not now. Then let me hear" +
161                 "Of you, my gentle cousin Westmoreland," +
162                 "What yesternight our council did decree" +
163                 "In forwarding this dear expedience.",
164 
165                 "Hear him but reason in divinity," +
166                 "And all-admiring with an inward wish" +
167                 "You would desire the king were made a prelate:" +
168                 "Hear him debate of commonwealth affairs," +
169                 "You would say it hath been all in all his study:" +
170                 "List his discourse of war, and you shall hear" +
171                 "A fearful battle render'd you in music:" +
172                 "Turn him to any cause of policy," +
173                 "The Gordian knot of it he will unloose," +
174                 "Familiar as his garter: that, when he speaks," +
175                 "The air, a charter'd libertine, is still," +
176                 "And the mute wonder lurketh in men's ears," +
177                 "To steal his sweet and honey'd sentences;" +
178                 "So that the art and practic part of life" +
179                 "Must be the mistress to this theoric:" +
180                 "Which is a wonder how his grace should glean it," +
181                 "Since his addiction was to courses vain," +
182                 "His companies unletter'd, rude and shallow," +
183                 "His hours fill'd up with riots, banquets, sports," +
184                 "And never noted in him any study," +
185                 "Any retirement, any sequestration" +
186                 "From open haunts and popularity.",
187 
188                 "I come no more to make you laugh: things now," +
189                 "That bear a weighty and a serious brow," +
190                 "Sad, high, and working, full of state and woe," +
191                 "Such noble scenes as draw the eye to flow," +
192                 "We now present. Those that can pity, here" +
193                 "May, if they think it well, let fall a tear;" +
194                 "The subject will deserve it. Such as give" +
195                 "Their money out of hope they may believe," +
196                 "May here find truth too. Those that come to see" +
197                 "Only a show or two, and so agree" +
198                 "The play may pass, if they be still and willing," +
199                 "I'll undertake may see away their shilling" +
200                 "Richly in two short hours. Only they" +
201                 "That come to hear a merry bawdy play," +
202                 "A noise of targets, or to see a fellow" +
203                 "In a long motley coat guarded with yellow," +
204                 "Will be deceived; for, gentle hearers, know," +
205                 "To rank our chosen truth with such a show" +
206                 "As fool and fight is, beside forfeiting" +
207                 "Our own brains, and the opinion that we bring," +
208                 "To make that only true we now intend," +
209                 "Will leave us never an understanding friend." +
210                 "Therefore, for goodness' sake, and as you are known" +
211                 "The first and happiest hearers of the town," +
212                 "Be sad, as we would make ye: think ye see" +
213                 "The very persons of our noble story" +
214                 "As they were living; think you see them great," +
215                 "And follow'd with the general throng and sweat" +
216                 "Of thousand friends; then in a moment, see" +
217                 "How soon this mightiness meets misery:" +
218                 "And, if you can be merry then, I'll say" +
219                 "A man may weep upon his wedding-day.",
220 
221                 "First, heaven be the record to my speech!" +
222                 "In the devotion of a subject's love," +
223                 "Tendering the precious safety of my prince," +
224                 "And free from other misbegotten hate," +
225                 "Come I appellant to this princely presence." +
226                 "Now, Thomas Mowbray, do I turn to thee," +
227                 "And mark my greeting well; for what I speak" +
228                 "My body shall make good upon this earth," +
229                 "Or my divine soul answer it in heaven." +
230                 "Thou art a traitor and a miscreant," +
231                 "Too good to be so and too bad to live," +
232                 "Since the more fair and crystal is the sky," +
233                 "The uglier seem the clouds that in it fly." +
234                 "Once more, the more to aggravate the note," +
235                 "With a foul traitor's name stuff I thy throat;" +
236                 "And wish, so please my sovereign, ere I move," +
237                 "What my tongue speaks my right drawn sword may prove.",
238 
239                 "Now is the winter of our discontent" +
240                 "Made glorious summer by this sun of York;" +
241                 "And all the clouds that lour'd upon our house" +
242                 "In the deep bosom of the ocean buried." +
243                 "Now are our brows bound with victorious wreaths;" +
244                 "Our bruised arms hung up for monuments;" +
245                 "Our stern alarums changed to merry meetings," +
246                 "Our dreadful marches to delightful measures." +
247                 "Grim-visaged war hath smooth'd his wrinkled front;" +
248                 "And now, instead of mounting barded steeds" +
249                 "To fright the souls of fearful adversaries," +
250                 "He capers nimbly in a lady's chamber" +
251                 "To the lascivious pleasing of a lute." +
252                 "But I, that am not shaped for sportive tricks," +
253                 "Nor made to court an amorous looking-glass;" +
254                 "I, that am rudely stamp'd, and want love's majesty" +
255                 "To strut before a wanton ambling nymph;" +
256                 "I, that am curtail'd of this fair proportion," +
257                 "Cheated of feature by dissembling nature," +
258                 "Deformed, unfinish'd, sent before my time" +
259                 "Into this breathing world, scarce half made up," +
260                 "And that so lamely and unfashionable" +
261                 "That dogs bark at me as I halt by them;" +
262                 "Why, I, in this weak piping time of peace," +
263                 "Have no delight to pass away the time," +
264                 "Unless to spy my shadow in the sun" +
265                 "And descant on mine own deformity:" +
266                 "And therefore, since I cannot prove a lover," +
267                 "To entertain these fair well-spoken days," +
268                 "I am determined to prove a villain" +
269                 "And hate the idle pleasures of these days." +
270                 "Plots have I laid, inductions dangerous," +
271                 "By drunken prophecies, libels and dreams," +
272                 "To set my brother Clarence and the king" +
273                 "In deadly hate the one against the other:" +
274                 "And if King Edward be as true and just" +
275                 "As I am subtle, false and treacherous," +
276                 "This day should Clarence closely be mew'd up," +
277                 "About a prophecy, which says that 'G'" +
278                 "Of Edward's heirs the murderer shall be." +
279                 "Dive, thoughts, down to my soul: here" +
280                 "Clarence comes.",
281 
282                 "To bait fish withal: if it will feed nothing else," +
283                 "it will feed my revenge. He hath disgraced me, and" +
284                 "hindered me half a million; laughed at my losses," +
285                 "mocked at my gains, scorned my nation, thwarted my" +
286                 "bargains, cooled my friends, heated mine" +
287                 "enemies; and what's his reason? I am a Jew. Hath" +
288                 "not a Jew eyes? hath not a Jew hands, organs," +
289                 "dimensions, senses, affections, passions? fed with" +
290                 "the same food, hurt with the same weapons, subject" +
291                 "to the same diseases, healed by the same means," +
292                 "warmed and cooled by the same winter and summer, as" +
293                 "a Christian is? If you prick us, do we not bleed?" +
294                 "if you tickle us, do we not laugh? if you poison" +
295                 "us, do we not die? and if you wrong us, shall we not" +
296                 "revenge? If we are like you in the rest, we will" +
297                 "resemble you in that. If a Jew wrong a Christian," +
298                 "what is his humility? Revenge. If a Christian" +
299                 "wrong a Jew, what should his sufferance be by" +
300                 "Christian example? Why, revenge. The villany you" +
301                 "teach me, I will execute, and it shall go hard but I" +
302                 "will better the instruction.",
303 
304                 "Virtue! a fig! 'tis in ourselves that we are thus" +
305                 "or thus. Our bodies are our gardens, to the which" +
306                 "our wills are gardeners: so that if we will plant" +
307                 "nettles, or sow lettuce, set hyssop and weed up" +
308                 "thyme, supply it with one gender of herbs, or" +
309                 "distract it with many, either to have it sterile" +
310                 "with idleness, or manured with industry, why, the" +
311                 "power and corrigible authority of this lies in our" +
312                 "wills. If the balance of our lives had not one" +
313                 "scale of reason to poise another of sensuality, the" +
314                 "blood and baseness of our natures would conduct us" +
315                 "to most preposterous conclusions: but we have" +
316                 "reason to cool our raging motions, our carnal" +
317                 "stings, our unbitted lusts, whereof I take this that" +
318                 "you call love to be a sect or scion.",
319 
320                 "Blow, winds, and crack your cheeks! rage! blow!" +
321                 "You cataracts and hurricanoes, spout" +
322                 "Till you have drench'd our steeples, drown'd the cocks!" +
323                 "You sulphurous and thought-executing fires," +
324                 "Vaunt-couriers to oak-cleaving thunderbolts," +
325                 "Singe my white head! And thou, all-shaking thunder," +
326                 "Smite flat the thick rotundity o' the world!" +
327                 "Crack nature's moulds, an germens spill at once," +
328                 "That make ingrateful man!"
329         };
330     }
331 
332     /**
333      * We will use a SpeechView to display each speech. It's just a LinearLayout
334      * with two text fields.
335      *
336      */
337     private class SpeechView extends LinearLayout {
SpeechView(Context context, String title, String words)338         public SpeechView(Context context, String title, String words) {
339             super(context);
340 
341             this.setOrientation(VERTICAL);
342 
343             // Here we build the child views in code. They could also have
344             // been specified in an XML file.
345 
346             mTitle = new TextView(context);
347             mTitle.setText(title);
348             addView(mTitle, new LinearLayout.LayoutParams(
349                     LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
350 
351             mDialogue = new TextView(context);
352             mDialogue.setText(words);
353             addView(mDialogue, new LinearLayout.LayoutParams(
354                     LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
355         }
356 
357         /**
358          * Convenience method to set the title of a SpeechView
359          */
setTitle(String title)360         public void setTitle(String title) {
361             mTitle.setText(title);
362         }
363 
364         /**
365          * Convenience method to set the dialogue of a SpeechView
366          */
setDialogue(String words)367         public void setDialogue(String words) {
368             mDialogue.setText(words);
369         }
370 
371         private TextView mTitle;
372         private TextView mDialogue;
373     }
374 }
375