栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android——SimpleAdapter的简单使用,感悟分享

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android——SimpleAdapter的简单使用,感悟分享

在Android中,Adapter(适配器)是数据与ui之间的桥梁,它把后台数据与前端ui连接到一起,是一个展示数据的载体。Adapter有很多的接口、抽象类、子类可以使用,以下是常用的几个适配器。

baseAdapter:是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性;

ArrayAdapter:支持泛型操作,最为简单,只能展示一行字。

SimpleAdapter:有最好的扩充性,可以自定义出各种效果。

SimpleCursorAdapter:可以适用于简单的纯文字型ListView,它需要Cursor的字段和UI的id对应起来。如需要实现更复杂的UI也可以重写其他方法。可以认为是SimpleAdapter对数据库的简单结合,可以方便地把数据库的内容以列表的形式展示出来。

这里,主要介绍SimpleAdapter的简单用法

2.SimpleAdapter


构造方法:

SimpleAdapter(Context context, List>data,int resource,String[ ] from, int[ ] to)

参数:

1.context:上下文。

2.data:基于Map的list。Data里边的每一项都和 ListView里边的每一项对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。

3.resource :就是一个布局layout,可引用系统提供的,也可以自定义。

4.from:这是个名字数组,每个名字是为了在 ArrayList数组的每一个item索引Map的Object用的。即 map 中得key值。

5.to:指定要填充的组件

3.SimpleAdapter实例——创建ListView


(1)main.xml

在布局文件中定义一个ListView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

android:id="@+id/mylist"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:divider="#000"

android:dividerHeight=“2dp”

android:listSelector="#600" />

(2)simpleadapter.xml

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:orientation=“horizontal”>

android:id="@+id/text"

android:layout_width=“300dp”

android:layout_height=“wrap_content”

android:padding=“25dp”

android:textSize=“20sp”

android:textColor="#000" />

android:id="@+id/image"

android:layout_width=“80dp”

android:layout_height=“80dp”

android:padding=“10dp” />

*(3)MainActivity.java

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 免费领取

package com.example.myapplication;

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/460071.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号