- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Android列表实现(3)_自定义列表适配器思路及实现代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
下面的例子为使用自定义的列表适配器来显示列表.
复制代码 代码如下
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列表实现(3)_自定义列表适配器思路及实现代码的文章就讲到这里了,如果你想了解更多关于Android列表实现(3)_自定义列表适配器思路及实现代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有 3 个 AutoCompleteTextView,我想在它们上面注册 2 个 String[] 适配器。目前,我正在这样做: atw_from.setAdapter(new ArrayAdapt
我需要实现一个 recyclerView 来显示我对 Parse 的查询,所以我已经做到了: private class Pagination extends RecyclerView.OnScro
我对 BizTalk 相当陌生,目前我只是探索它的功能并了解不同部分(架构、编排、端口等)如何协同工作。我对其适配器有疑问: 不同的适配器是否已经随 BizTalk 服务器安装一起预装并准备好配置,或
我在 BizTalk 中测试 MQSC 适配器以与 Z/OS 主机上的队列通信时遇到问题。 测试场景:通过 Biztalk 发送消息时,我(强制)停止并启动主机 channel ,以模拟主机 IPL。
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我想用我的音频单元在iPhone上录制一条音频信号,该信号来自一条普通的3.5毫米音频电缆(例如,另一部iPhone作为声源)。 由于iPhone具有4端口耳机插孔,因此无法直接插入。 我尝试了不同种
[请参阅下面的更新] 我很难定义模式。我的同事说这是适配器模式。我不知道。我们陷入困境主要是因为我们想要正确命名我们的组件。 问题:是适配器模式吗?如果不是的话是什么?如果是其他事情,这是实现这个想法
我有点不熟悉Java KeyAdapter有效,并且使用 KeyAdapter 使用以下代码得到了意想不到的结果。当按下一个键而另一个键已按下时,就会出现此问题,无论 isKeyPressed() 是
我想知道如何通过 ORM 适配器使用 Node.js 在 MySQL 中创建多个表。我通过模型创建了一个表,即“us.js” module.exports = { identity: 'us'
我有一个 JavaFx 客户端。我正在使用一个具有 ObservableSet 作为字段的 bean 作为模型。我想将这些数据显示到 ListView 中,但我无法将我的字段类型更改为 Observa
我正在尝试在 native iOS 应用程序中实现基于表单的身份验证,但我需要在没有收到质询的情况下登录,我想打开一个表单并登录。我实现了一个包含 isCustomResponse 函数的 Chall
我正在尝试为我的迭代器和 const_iterator 类实现反向迭代器适配器,但遇到了一些麻烦。如果有人可以指导我解决这个问题,将不胜感激! 我的想法是我应该能够从我的 rbegin() 和 ren
使用 spring-integration-sftp,创建任意数量的入站 channel 适配器对象的推荐方法是什么?我的应用程序需要监视多个远程目录(1 到 n),直到运行时才知道。 最佳答案 当前
我正在尝试为我们自己的框架创建适配器。我们的框架使用自己的断言机制,因此我需要编写适配器。 适配器类非常简单,如下所示: public class AllureReportListener {
有没有什么方法可以使用命令行而不是使用 Worklight 控制台来部署 Worklight 适配器? (因为我的 worklight 服务器安装在 WAS 上,wsadmin 命令或类似的命令...
我想构建自己的自定义 log4j(网络)适配器来解决我的问题 that I posted here. 我查看了 log4j 的文档,但看不到开发人员在哪里/是否讨论如何执行此操作。 有人能给我指出正确
我使用消息驱动 channel 适配器从 weblogic JMS 队列接收作为字符串的 xml 消息,然后将此消息传递到 spring 集成 channel 以存储到数据库中,转换为不同的 xml,
有没有什么方法可以使用命令行而不是使用 Worklight 控制台来部署 Worklight 适配器? (因为我的 worklight 服务器安装在 WAS 上,wsadmin 命令或类似的命令...
我试图为 Android 制作一个聊天应用程序,所以我使用了 RecyclerView 。我的适配器有问题,我的聊天室收到的 JSON 响应显示为空白。我的代码是否遗漏了某些内容? 这是我的适配器类
如果这是重复的,我提前道歉。我对 Android 开发还是新手,并尝试寻找解决方案,但找不到有效的解决方案。 我正在创建一个待办事项应用程序并在我的适配器中收到此错误。 java.lang.NullP
我是一名优秀的程序员,十分优秀!