The following example shows a list using a custom list adapter.
Copy Code code as follows:
View Code
Import Android.os.Bundle;
Import android.app.ListActivity;
Import Android.content.Context;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
public class Mainactivity extends Listactivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setlistadapter (This) (new Speechlistadapter);
}
/**
* A Sample ListAdapter that presents content from arrays of speeches and
* Text.
*
*/
Private class Speechlistadapter extends Baseadapter {
Public Speechlistadapter {
Mcontext = context;
}
/**
* The number of items in the ' list is ' determined by the number of speeches
* in our array.
*
* @see Android.widget.listadapter#getcount ()
*/
public int GetCount () {
return mtitles.length;
}
/**
* Since The data comes from a array, just returning the index is
* Sufficent to get at the data. If we were using a more complex data
* Structure, we would return whatever object represents one row in the
* list.
*
* @see Android.widget.listadapter#getitem (int)
*/
Public Object getitem (int position) {
return position;
}
/**
* Use the array index as a unique ID.
*
* @see android.widget.listadapter#getitemid (int)
*/
public long getitemid (int position) {
return position;
}
/**
* Make a speechview to hold each row.
*
* @see android.widget.listadapter#getview (int, Android.view.View,
* Android.view.ViewGroup)
*/
Public View GetView (int position, View Convertview, ViewGroup parent) {
Speechview SV;
if (Convertview = = null) {
SV = new Speechview (Mcontext, Mtitles[position],
Mdialogue[position]);
} else {
SV = (speechview) Convertview;
Sv.settitle (Mtitles[position]);
Sv.setdialogue (Mdialogue[position]);
}
return SV;
}
/**
* Remember We can use it when the constructing views.
*/
Private context Mcontext;
/**
* Our data, Part 1.
*/
Private string[] Mtitles =
{
"Henry IV (1)",
"Henry V",
"Henry VIII,"
"Richard II",
"Richard III",
"Merchant of Venice",
"Othello",
"King Lear."
};
/**
* Our data, Part 2.
*/
Private string[] Mdialogue =
{
"So shaken as we are, so wan with care," +
"Find we a time for frighted peace to pant," +
"And breathe short-winded accents of new broils" +
"To being commenced in Strands Afar remote." +
"No more The thirsty entrance of this soil" +
"Shall daub her lips with her own children ' s blood;"
"Nor more shall trenching War channel her fields," +
"Nor bruise her flowerets with the armed hoofs" +
"of hostile paces:those opposed eyes," +
"Which, like the meteors of a troubled heaven," +
"All of one nature, of one substance bred," +
"Did lately meet in the intestine shock" +
"and furious close to civil butchery" +
"Shall now, in mutual well-beseeming ranks," +
"March all one way and is no more opposed" +
"Against acquaintance, Kindred and allies:" +
"The edge of war, like an ill-sheathed knife," +
"No more shall cut his master." Therefore, friends, "+
"As far as to the sepulchre of Christ," +
"Whose soldier now, under whose blessed cross" +
"We are impressed and engaged to fight," +
"Forthwith a power of 中文版 shall we levy;"
"Whose arms were moulded in their mothers ' womb" +
"To chase this pagans in those holy fields" +
"Over whose acres walk ' d those blessed feet" +
"Which Fourteen hundred years ago were nail ' d" +
"For our advantage on the bitter cross."
"But This we purpose now be twelve month old," +
"and Bootless ' tis to tell you we'll go:" +
"Therefore we meet not now." Then let me hear "+
"Of your, my gentle cousin Westmoreland," +
"What yesternight Our council did decree" +
"In forwarding this dear expedience.",
"Hear him but reason in divinity," +
"and all-admiring with a inward wish" +
"You would desire the king were made a prelate:" +
"Hear him debate of Commonwealth affairs," +
"You are would say it hath been all in and his study:" +
"List His Discourse of the war, and you shall hear" +
"A fearful battle render ' d in music:" +
"Turn him to any cause of policy," +
"The Gordian knot of it he would unloose," +
"Familiar as his garter:that, and he speaks," +
"The air, a charter ' d libertine, is still," +
"And the mute wonder Lurketh in men ' s ears," +
"To steal his sweet and honey ' d sentences;"
"So," art and practic part of life "+
"Must be the mistress to this theoric:" +
"Which is a wonder how he grace should glean it," +
"Since His addiction is to courses vain," +
"His companies Unletter ' d, rude and shallow," +
"His hours fill ' d-with riots, banquets, sports," +
"And never noted in him any study," +
"Any retirement, any sequestration" +
"From open haunts and popularity.",
"I Come No." You laugh:things now, "+
"That bear a weighty and a serious brow," +
"Sad, High, and working, all state and woe," +
"Such Noble scenes as draw the eye to flow," +
"We now present." Those that can pity, here "+
"May, if they, it's OK, let fall a tear;"
"The subject would deserve it. Such as give "+
"Their-hope they may believe," +
May I find truth too. Those that come to "+
"A show or two, and so agree" +
"The play could pass, if they is still and willing," +
"I ll undertake may" away their shilling "+
"Richly in two, short hours. Only they "+
"That's come to hear a merry bawdy play," +
"A noise of targets, or to a fellow" +
"In a long motley coat guarded with yellow," +
"would be deceived; For, gentle hearers, know, "+
"To rank we chosen truth with such a" +
"As Fool and fight IS, beside forfeiting" +
"Our own brains, and the opinion we bring," +
"To" is true we now intend, "+
"Would leave us never an understanding friend." +
"Therefore, for goodness ' sake, and as to you are known" +
"The happiest hearers of the town," +
"Being sad, as we would make ye:think ye" +
"The very persons of our noble story" +
"As they were living; I'm them great, "+
"and follow ' d with the general throng and sweat" +
"of thousand friends; Then in a moment, "+"
"How soon this mightiness meets misery:" +
"And, if you can be merry then, I ' ll say" +
"A man may weep upon his wedding-day."
"The record of the Heaven be the" "I speech!" +
"In the devotion of a subject's love," +
"Tendering the precious safety of my Prince," +
"And free from the other misbegotten hate," +
"Come I appellant to this princely presence."
"Now, Thomas Mowbray, does I turn to thee," +
"and mark my greeting; For what I speak "+
"My body shall make good upon the This earth," +
"Or my divine Soul answer it in heaven." +
"Thou Art a traitor and a miscreant," +
"Too good to Being and Too to live," +
"Since the more fair and Crystal are the sky," +
"The uglier seem the clouds in it fly."
"Once more," the aggravate the note, "+
"With a foul traitor ' s name stuff I thy throat;"
"and wish, so I am sovereign, Ere I move," +
"What my tongue speaks I right drawn sword may prove.",
"Now are the winter of our discontent" +
"Made Glorious summer by this sun of York;"
"And all of the clouds that Lour ' d upon we House" +
"In the deep bosom of the ocean buried." +
"Now are we brows bound with victorious wreaths;"
"Our bruised arms hung up for monuments;"
"We stern alarums changed to merry meetings," +
"We dreadful marches to delightful measures."
"Grim-visaged War hath smooth ' d his wrinkled front;"
"And now, instead of mounting barded steeds" +
"To fright the souls of fearful adversaries," +
"He capers nimbly in a lady ' s chamber" +
"To the lascivious pleasing of a lute." +
"But I, that am not shaped for sportive tricks," +
"Nor made to court a amorous looking-glass;"
"I, that's AM rudely stamp ' d, and want Love ' s Majesty" +
"To strut before a wanton ambling nymph;"
"I, that's am curtail ' d of this fair proportion," +
"Cheated of feature by dissembling nature," +
"Deformed, Unfinish ' d, sent before my time" +
"Into this breathing world, the scarce half made up," +
"And so lamely and unfashionable" +
"That dogs bark in me as I halt by them;"
"Why, I, in-weak piping time of peace," +
"Have no delight to pass away", "+
"Unless to spy me Shadow in the Sun" +
"and descant on Mine own deformity:" +
"and therefore, since I cannot prove a lover," +
"To entertain those fair well-spoken days," +
"I am determined to prove a villain" +
"And hate the idle pleasures."
"Plots have I laid, inductions dangerous," +
"By drunken prophecies, libels and dreams," +
"To set my brother Clarence and the King" +
"In deadly hate the One against:" +
"And if King Edward as true and just" +
"As I am subtle, false and treacherous," +
"This day should Clarence closely is Mew ' d up," +
"About a prophecy, which says that ' G '" +
"Edward ' s heirs the murderer shall be." +
"Dive, thoughts, down to my soul:here" +
"Clarence comes."
"To bait fish withal:if It'll feed nothing else," +
"It'll feeds my revenge." He hath disgraced me, and "+
"Hindered me half a million; Laughed at my losses, "+
"Mocked at my gains, scorned me nation, thwarted my" +
"Bargains, cooled my friends, heated mine" +
"Enemies; And what ' s his reason? I am a Jew. Hath "+
"Not a Jew eyes?" Hath not a Jew hands, organs, "+
"Dimensions, senses, affections, passions?" Fed with "+
"The same food, hurt with the same weapons, subject" +
"To the same diseases, healed by the same means," +
"Warmed and cooled by the same winter and summer, as" +
"A Christian is?" If you are prick us, do we don't bleed? "+
"If you tickle us, does we not laugh?" If you poison "+
"Us, do we don't die?" And if you wrong us, shall we are +
"Revenge?" If We are like you in the rest, we'll "+
"Resemble you." If a Jew wrong a Christian, "+
"What is his humility?" Revenge. If a Christian "+
"Wrong a Jew, what should his sufferance is by" +
"Christian example?" Why, Revenge. The villany You "+
"Teach me, I'll execute, and it shall go hard but I" +
"Would better the instruction."
"Virtue! A fig! ' Tis in ourselves that we are thus ' +
"or thus. Our bodies are we gardens, to the which "+
"Our wills are Gardeners:so is that if we'll plant" +
"Nettles, or sow lettuce, set hyssop and weed up" +
"Thyme, supply it with one gender of herbs, or" +
"Distract it with many, either to have it sterile" +
"With idleness, or manured with industry, why," +
"Power and corrigible authority of the This lies" +
"Wills. If the balance of our lives had is not one "+
"Scale of reason to poise another of sensuality," +
"Blood and baseness of our natures would conduct us" +
"To most preposterous conclusions:but we have" +
"Reason to cool we raging motions, our carnal" +
"Stings, our unbitted lusts, whereof I Take the This" +
"You called Love to be a sect or scion."
"Blow, winds, and crack your cheeks! rage! blow! "+
"You cataracts and hurricanoes, spout" +
"Till you have drench ' d our steeples, drown ' d the cocks!" +
"You sulphurous and thought-executing fires," +
"Vaunt-couriers to Oak-cleaving Thunderbolts," +
"Singe my white head! And thou, All-shaking thunder, "+
"Smite flat The Thick rotundity o ' world!" +
"Crack nature's moulds, an germens spill at once," +
"That's make ingrateful man!"
};
}
/**
* We use a speechview to display each speech. It ' s just a linearlayout
* with two text fields.
*
*/
Private class Speechview extends LinearLayout {
Public Speechview (context context, string title, string words) {
Super (context);
This.setorientation (VERTICAL);
Here we do the child views in code. They could also have
been specified in an XML file.
Mtitle = new TextView (context);
Mtitle.settext (title);
AddView (Mtitle, New Linearlayout.layoutparams (
Layoutparams.match_parent, layoutparams.wrap_content));
Mdialogue = new TextView (context);
Mdialogue.settext (words);
AddView (Mdialogue, New Linearlayout.layoutparams (
Layoutparams.match_parent, layoutparams.wrap_content));
}
/**
* Convenience to set the title of a Speechview
*/
public void Settitle (String title) {
Mtitle.settext (title);
}
/**
* Convenience to set the dialogue of a Speechview
*/
public void Setdialogue (String words) {
Mdialogue.settext (words);
}
Private TextView Mtitle;
Private TextView Mdialogue;
}
}
In this example, the adapter Speechlistadapter (which inherits the parent class Baseadapter) and the Speechview (whose parent class is LinearLayout) as a record template are customized.
The method in Speechlistadapter GetView implements the logic of the record display object that returns the specified position.