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