ListView 适配器 SimpleAdapter 和 SimpleCursorAdapter 细节总结 ListView 是 Android 中最常用的列表控件之一,而适配器(Adapter)是 ListView 的数据提供者。今天,我们将深入探讨两个常用的 ListView 适配器:SimpleAdapter 和 SimpleCursorAdapter。 SimpleAdapter SimpleAdapter 是一个通用的 Adapter,它可以将数据绑定到 ListView 中。它的构造函数为: public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) 其中: * context:是当前 ListView 所在的 Context。 * data:是一个 List,保存着要显示在 ListView 中的数据,每个数据项是一个 Map 对象。 * resource:是 ListView 中每个条目的布局文件。 * from:是一个字符串数组,指定了 ListView 中每个条目的列名。 * to:是一个整数数组,指定了每个条目中的控件的 ID。 SimpleAdapter 适配器关联的数据保存在 List<Map<?,?>> 中,每个 Map 对象代表一个数据项,键为列名,值为该列的数据。 SimpleCursorAdapter SimpleCursorAdapter 是另外一个常用的 Adapter,它可以将数据库中的数据绑定到 ListView 中。它的构造函数与 SimpleAdapter 相似,但它关联的数据保存在 Cursor 对象中。 Cursor 对象是 Android 中用于存储和操作数据库数据的对象。SimpleCursorAdapter 需要 Cursor 对象中包含一个名为 "_id" 的字段,如果表中没有这个字段,可以使用别名来解决问题。 例如: Cursor cursor = db.rawQuery("select personId as _id, name from person where personId = ?", new String[]{person.getPersonId()}); 在上面的例子中,我们使用了别名 "personId as _id" 来将 personId 字段命名为 "_id"。 两者的比较 SimpleAdapter 和 SimpleCursorAdapter 都可以用来绑定数据到 ListView 中,但它们有所不同: * SimpleAdapter 适合用于绑定小量的、静态的数据,而 SimpleCursorAdapter 更适合用于绑定大量的、动态的数据。 * SimpleAdapter 的数据保存在 List<Map<?,?>> 中,而 SimpleCursorAdapter 的数据保存在 Cursor 对象中。 * SimpleCursorAdapter 需要 Cursor 对象中包含一个名为 "_id" 的字段,而 SimpleAdapter 不需要。 SimpleAdapter 和 SimpleCursorAdapter 是两个常用的 ListView 适配器,它们都可以用来绑定数据到 ListView 中,但它们有所不同的使用场景和特点。
















- jinlingyunhuhu2013-08-12还可以,简单比较

- 粉丝: 189
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- (2025)高压电工作业复审考试试题库与答案.docx
- (2025)高校单招考试试题及答案.docx
- (2025)高校单招考试试题与答案.docx
- (2025)高校管理岗笔试题目及答案.docx
- (2025)高校管理岗笔试题目与答案.docx
- (2025)高压电工作业考试试题库与答案.docx
- (2025)高职单招英语考试题(带答案).docx
- (2025)高职单招英语考试题(含答案).docx
- (2025)高职单招语文试卷和答案.docx
- (2025)高职单招语文试卷及答案.docx
- (2025)高中学业水平考试卷生物试题(含答案).docx
- (2025)高中语文教师资格面试题(附答案).docx
- (2025)个人违规吃喝自查自纠报告.docx
- (2025)工厂安全知识竞赛题库及答案.docx
- (2025)工厂安全生产知识竞赛题库及答案.docx
- (2025)公基础知识试题与答案(2025版).docx


