下面的例子为使用自定义的列表适配器来显示列表。

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(new SpeechListAdapter(this));

}

/**

* A sample ListAdapter that presents content from arrays of speeches and

* text.

*

*/

private class SpeechListAdapter extends BaseAdapter {

public SpeechListAdapter(Context context) {

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 an 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 our context so we can use it when 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 be 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 of civil butchery" +

"Shall now, in mutual well-beseeming ranks," +

"March all one way and be 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 English shall we levy;" +

"Whose arms were moulded in their mothers' womb" +

"To chase these 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 our purpose now is twelve month old," +

"And bootless 'tis to tell you we will go:" +

"Therefore we meet not now. Then let me hear" +

"Of you, 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 an inward wish" +

"You would desire the king were made a prelate:" +

"Hear him debate of commonwealth affairs," +

"You would say it hath been all in all his study:" +

"List his discourse of war, and you shall hear" +

"A fearful battle render'd you in music:" +

"Turn him to any cause of policy," +

"The Gordian knot of it he will unloose," +

"Familiar as his garter: that, when 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 that the art and practic part of life" +

"Must be the mistress to this theoric:" +

"Which is a wonder how his grace should glean it," +

"Since his addiction was to courses vain," +

"His companies unletter'd, rude and shallow," +

"His hours fill'd up with riots, banquets, sports," +

"And never noted in him any study," +

"Any retirement, any sequestration" +

"From open haunts and popularity.",

"I come no more to make you laugh: things now," +

"That bear a weighty and a serious brow," +

"Sad, high, and working, full of state and woe," +

"Such noble scenes as draw the eye to flow," +

"We now present. Those that can pity, here" +

"May, if they think it well, let fall a tear;" +

"The subject will deserve it. Such as give" +

"Their money out of hope they may believe," +

"May here find truth too. Those that come to see" +

"Only a show or two, and so agree" +

"The play may pass, if they be still and willing," +

"I'll undertake may see away their shilling" +

"Richly in two short hours. Only they" +

"That come to hear a merry bawdy play," +

"A noise of targets, or to see a fellow" +

"In a long motley coat guarded with yellow," +

"Will be deceived; for, gentle hearers, know," +

"To rank our chosen truth with such a show" +

"As fool and fight is, beside forfeiting" +

"Our own brains, and the opinion that we bring," +

"To make that only true we now intend," +

"Will leave us never an understanding friend." +

"Therefore, for goodness' sake, and as you are known" +

"The first and happiest hearers of the town," +

"Be sad, as we would make ye: think ye see" +

"The very persons of our noble story" +

"As they were living; think you see them great," +

"And follow'd with the general throng and sweat" +

"Of thousand friends; then in a moment, see" +

"How soon this mightiness meets misery:" +

"And, if you can be merry then, I'll say" +

"A man may weep upon his wedding-day.",

"First, heaven be the record to my speech!" +

"In the devotion of a subject's love," +

"Tendering the precious safety of my prince," +

"And free from other misbegotten hate," +

"Come I appellant to this princely presence." +

"Now, Thomas Mowbray, do I turn to thee," +

"And mark my greeting well; for what I speak" +

"My body shall make good upon this earth," +

"Or my divine soul answer it in heaven." +

"Thou art a traitor and a miscreant," +

"Too good to be so and too bad to live," +

"Since the more fair and crystal is the sky," +

"The uglier seem the clouds that in it fly." +

"Once more, the more to aggravate the note," +

"With a foul traitor's name stuff I thy throat;" +

"And wish, so please my sovereign, ere I move," +

"What my tongue speaks my right drawn sword may prove.",

"Now is the winter of our discontent" +

"Made glorious summer by this sun of York;" +

"And all the clouds that lour'd upon our house" +

"In the deep bosom of the ocean buried." +

"Now are our brows bound with victorious wreaths;" +

"Our bruised arms hung up for monuments;" +

"Our stern alarums changed to merry meetings," +

"Our 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 an amorous looking-glass;" +

"I, that am rudely stamp'd, and want love's majesty" +

"To strut before a wanton ambling nymph;" +

"I, that am curtail'd of this fair proportion," +

"Cheated of feature by dissembling nature," +

"Deformed, unfinish'd, sent before my time" +

"Into this breathing world, scarce half made up," +

"And that so lamely and unfashionable" +

"That dogs bark at me as I halt by them;" +

"Why, I, in this weak piping time of peace," +

"Have no delight to pass away the time," +

"Unless to spy my shadow in the sun" +

"And descant on mine own deformity:" +

"And therefore, since I cannot prove a lover," +

"To entertain these fair well-spoken days," +

"I am determined to prove a villain" +

"And hate the idle pleasures of these days." +

"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 the other:" +

"And if King Edward be as true and just" +

"As I am subtle, false and treacherous," +

"This day should Clarence closely be mew'd up," +

"About a prophecy, which says that 'G'" +

"Of Edward's heirs the murderer shall be." +

"Dive, thoughts, down to my soul: here" +

"Clarence comes.",

"To bait fish withal: if it will feed nothing else," +

"it will feed my revenge. He hath disgraced me, and" +

"hindered me half a million; laughed at my losses," +

"mocked at my gains, scorned my 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 prick us, do we not bleed?" +

"if you tickle us, do we not laugh? if you poison" +

"us, do we not die? and if you wrong us, shall we not" +

"revenge? If we are like you in the rest, we will" +

"resemble you in that. If a Jew wrong a Christian," +

"what is his humility? Revenge. If a Christian" +

"wrong a Jew, what should his sufferance be by" +

"Christian example? Why, revenge. The villany you" +

"teach me, I will execute, and it shall go hard but I" +

"will better the instruction.",

"Virtue! a fig! 'tis in ourselves that we are thus" +

"or thus. Our bodies are our gardens, to the which" +

"our wills are gardeners: so that if we will 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, the" +

"power and corrigible authority of this lies in our" +

"wills. If the balance of our lives had not one" +

"scale of reason to poise another of sensuality, the" +

"blood and baseness of our natures would conduct us" +

"to most preposterous conclusions: but we have" +

"reason to cool our raging motions, our carnal" +

"stings, our unbitted lusts, whereof I take this that" +

"you call 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' the world!" +

"Crack nature's moulds, an germens spill at once," +

"That make ingrateful man!"

};

}

/**

* We will 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 build 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 method to set the title of a SpeechView

*/

public void setTitle(String title) {

mTitle.setText(title);

}

/**

* Convenience method to set the dialogue of a SpeechView

*/

public void setDialogue(String words) {

mDialogue.setText(words);

}

private TextView mTitle;

private TextView mDialogue;

}

}

该例子中自定义了适配器 SpeechListAdapter (其继承父类BaseAdapter)以及作为记录模板的 SpeechView(其父类为LinearLayout)。

SpeechListAdapter中的方法getView实现了返回指定位置的记录显示对象的逻辑。

android自定义列表显示,Android列表实现(3)_自定义列表适配器思路及实现代码相关推荐

  1. android 自定义view实现拖动放大缩小_自定义itemClickView

    和你一起终身学习,这里是程序员Android 经典好文推荐,通过阅读本文,您将收获以下知识点: 一.自定义View类实现 二.自定义View标签 三.自定义View 布局 四.自定义View 选择器 ...

  2. android 自定义view实现拖动放大缩小_自定义itemCheckView

    阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容: 自定义View类实现 自定义View标签 ...

  3. android 定义数组,Android列表实现(1)_数组列表实例介绍

    最近开始学习android的ui,先上几个相关的例子,后续还会有更新. import android.app.ListActivity; import android.os.Bundle; impor ...

  4. 两个列表合并去重_数据结构——列表

    4.1列表的概念 列表(list)是用来存储一组有序数据元素的数据结构,元素之间用逗号分隔.列表中的数据元素应该包括在方括号中,而且列表是可变的数据类型,一旦创建了一个列表,你可以添加.删除或者搜索列 ...

  5. python列表查找值_查找列表中某个值的位置(python)

    p=list.index(value) list为列表的名字 value为查找的值 p为value在list的位置 Python3.2.2列表操作总结 list操作:快速创建list.新增item.删 ...

  6. JAVA散列表个人通讯录_散列表实现简易通讯录

    散列表实现通讯录 1.项目研究背景与意义 背景:随着信息活动在国民经济中主导地位的确立和信息产业的崛起,信息资源管理作为一个专有名词和独立的学科逐渐发展起来.如何积极开发.合理配置和有效利用信息资源, ...

  7. 迈普路由器访问控制列表配置命令_访问控制列表ACL及配置教程

    访问控制列表ACL及配置教程 发布时间:2012-11-28 17:04:38   作者:佚名   我要评论 本文将详细介绍访问控制列表ACL及配置,需要了解更多的朋有可以参考下 访问控制列表: AC ...

  8. web无序列表去掉点_无序列表ul标记的type属性值可以为-智慧树Web前端开发技术(山东联盟)章节答案...

    Web前端开发技术(山东联盟):无序列表ul标记的type属性值可以为[多选题] A:square B:circle C:disc D:line Web前端开发技术(山东联盟)章节测试答案: squa ...

  9. python列表修改数据_使用列表理解修改数据框架列

    我有一个包含大约90k个字符串的列表和一个包含多个列的数据框,我想检查列表中的一个字符串是否在列1中,以及是否在列2分配了相同的值. 我可以做到:for i in range(len(my_list) ...

最新文章

  1. layui 日历选中日期,自动填充
  2. 【MATLAB】变量 ( 特殊变量和常量 | 关键字 | 调用优先级 | 数值显示格式 )
  3. 自由自在公司解读休闲食品的五大发展趋势
  4. 汇编原理实验 --类型统计(字母,数字,其他字符的个数)
  5. 《Linux内核分析》课程总结
  6. php答题评分,Thinkphp 答题 评语
  7. c语言标准图形库,C语言图形库函数.docx
  8. javascript教程_JavaScript教程
  9. 张亚勤退休百度,技术客回归学术
  10. 分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(12月5日-12月11日)
  11. 健脾和胃,养生食疗——山药枸杞鲫鱼汤了解一下
  12. 什么是Hadoop以及Hadoop集群
  13. 关于mac重启/home目录丢失解决方案
  14. java实现海盗比酒量
  15. 计算Fisher信息之基础矩阵(一)
  16. ORTP移植到Hi3518e,h.264封包rtp发送
  17. 微型计算机期末考试a卷,微机原理与接口技术期末试卷A
  18. 关于嵌入式工程师薪资待遇
  19. Docker部署web项目
  20. poi直接将excel出力为pdf或者text

热门文章

  1. java多态主要体现在哪方面,浅谈java多态的实现主要体现在哪些方面
  2. C#上位机 串口上位机Modbus协议
  3. Python连接数据库pyodbc
  4. A Better Finder Rename 11 for Mac(批量重命名工具)
  5. Asp.net 处理程序(第五篇)
  6. Kibana KQL语法
  7. docker 搭建以太坊私有链搭建
  8. PHP解说日全食红月
  9. python编程图书批发商店的某本书的零售价是26.5_根据实验指导书,完成实验3的所有任务,使用 实验三(实验报告).docx 完成报告并提交。_学小易找答案...
  10. 利用SpiderMonkey进行嵌入式开发——学习总结