android 适配器 ArrayAdapter,SimpleAdapter的学习
今天认真看了下android适配器,学习了下它的使用方法。
一,ArrayAdapter
ArrayAdapter 比较简单,只可以存放一行文本信息。下面是简单的实现
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_array_adapter);
listView = (ListView)findViewById(R.id.listView3);
list = new ArrayList<String>();
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,getData());
listView.setAdapter(adapter);
}
ArrayList<String> getData(){
list.add("第一行");
list.add("第二行");
list.add("第三行");
list.add("第四行");
return list;
}
1,首先需要定义一个数组的集合,这里用ArrayList<String>类型,通过getData()动态的添加数据,
2,定义一个ArrayAdapter,参数context :只需要填入上下文this,第二个参数 resource:是一个布局文件,第三个参数List<T> objects:是一个数据集合。上面代码中的
android.R.layout.simple_list_item_1参数是一个系统自带的laout文件,通过按键ctrl+B查看源代码如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
只是一个简单的TextView,垂直居中,当然如果不用系统自带的,用我们自己创建的也可以。
3,将适配器和控件进行绑定。如果没有这一步,相当于我们创建的控件是不会显示出任何数据的。
二 SimpleAdapter
SimpleAdapter 比ArrayAdapter稍微复杂一些,一般用在图片文字混排的数据结构中一个简单例子
private SimpleAdapter adapter;
private ListView listView;
private List<Map<String,Object>> list; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_adapter);
listView = (ListView)findViewById(R.id.listView2);
list = new ArrayList<Map<String, Object>>();
adapter = new SimpleAdapter(this,getData(),R.layout.itemlayout,new String[]{"pic","title","content"},new int[]{R.id.imageView,R.id.textView,R.id.textView2});
listView.setAdapter(adapter);
}
List<Map<String,Object>> getData(){
Map<String,Object> map = new HashMap<String, Object>();
map.put("pic",R.drawable.a);
map.put("title","标题");
map.put("content","内容");
list.add(map);
map = new HashMap<String, Object>();
map.put("pic",R.drawable.b);
map.put("title","标题");
map.put("content","内容");
list.add(map);
map = new HashMap<String, Object>();
map.put("pic",R.drawable.c);
map.put("title","标题");
map.put("content","内容");
list.add(map);
map = new HashMap<String, Object>();
map.put("pic",R.drawable.d);
map.put("title","标题");
map.put("content","内容");
list.add(map);
return list;
}
通过ctrl+B 查看SimpleAdapter 定义,其中参数
List<? extends Map<String, ?>> data是一个List的集合,成员是一个Map,
resource 同样是一个布局文件,只是这里的布局文件需要我们手动添加,
from map中映射到控件ID的字符串
to 就是我们定义的控件ID
public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
这里的子布局文件使用自定义itemlayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="60dp"
android:id="@+id/imageView" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView2" />
</LinearLayout>
</LinearLayout>
最后一点就是千万不能忘记setAdapter
android 适配器 ArrayAdapter,SimpleAdapter的学习的更多相关文章
- Android学习之适配器ArrayAdapter SimpleAdapter
Adapter是个什么角色呢?其实它的作用就是View界面和数据之间的桥梁.我们可以看作是界面数据绑定的一种理解,它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等. 常用的适配器 ...
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- 13.Android-ListView使用、BaseAdapter/ArrayAdapter/SimpleAdapter适配器使用
1.ListView ListView 是 Android 系统为我们提供的一种列表显示的一种控件,使用它可以用来显示我们常见的列表形式.继承自抽象类 AdapterView.继承图如下所示: 以微信 ...
- Android 常用数据适配器ArrayAdapter
接着上篇文章<Android 采用Layout Inflater创建一个View对象>,本文采用常用数据适配器ArrayAdapter 新建项目后,在layout文件夹下新建list_it ...
- android 71 ArrayAdapter和SimpleAdapter
Activity和item: Activity:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/an ...
- android 适配器simpleadapter和baseadapter区别
android 适配器 simpleadapter 和 baseadapter 设计网络程序或者数据处理显示程序的时候,常常会使用 simpleadapter 和baseadapter 来实现. ad ...
- android适配器及监听点击和滚动在ListView中的使用
package com.example.demon08; import java.util.ArrayList;import java.util.HashMap;import java.util.Li ...
- Android ListView ArrayAdapter 的简单使用
前面写了3篇关于android的文章,其中的演示程序都写在了一个工程中,当时为了方便测试就在启动页MainActivity中放了3个按钮,点击不同的按钮进入不同的示例程序页面,MainActivity ...
- android之ArrayAdapter的重写
昨天介绍了ArrayAdapter的使用,今天介绍一下更加实用的一点,对它进行重写,满足自己的个性化设计需要. ArrayAdapter(数组适配器)一般用于显示一行文本信息,所以比较容易. publ ...
随机推荐
- web前段2017.6.8
<body></body>background='图片路径'---表示背景图片图片:.jpg .png(透明图片) .gif(动态图)... 路径---绝对路径:相对于磁盘的路 ...
- CSS学习(页外引用还不懂)
CSS的语法结构为 选择符 {属性:值:} Selector {Property : Value:} 选择符:通配 *{....} , 元素 body{....} .h1{....}.p ...
- MySql单表最大8000W+ 之数据库遇瓶颈记
前言 昨晚救火到两三点,早上七点多醒来,朦胧中醒来发现电脑还开着,赶紧爬起来看昨晚执行的SQL命令结果.由于昨晚升级了阿里云的RDS,等了将近两个小时 还在 升降级中,早上阿里云那边回复升级过程中出现 ...
- 阅读MDN文档之布局(四)
Introducing positioning Static positioning Relative positioning Introducing top, bottom, left and ri ...
- Go语言string,int,int64 ,float转换
(1)int转string s := strconv.Itoa(i)等价于s := strconv.FormatInt(int64(i), 10) (2)int64转string i := int64 ...
- Ambari2.5.3卸载smartsense
第一步,确定SmartSence服务均已关闭 curl -u admin:$PASSWORD -i -H 'X-Requested-By: ambari' -X PUT -d '{"Requ ...
- Log4Net(一):快速入门
概览 Log4Net是Apache Log4J框架在.NET平台上的实现,它是一个帮助开发者将日志信息以多种方式(数据库.控制台.文件等)输出的开源工具. 为什么要使用日志记录 提供应用程序运行时状态 ...
- JARVIS 手机监控局域网内PC
JARVIS 通过反向的Socket连接,实现通过手机(或任何可以发送Http请求的设备)对局域网内PC的监控.在外时可以远程监视家里PC任务的执行情况,甚至远程唤醒家里的PC提前打开游戏也可以实现( ...
- c#中Class和Struct使用与性能的区别
在Unity中很多已经定义为结构体的数据结构 Vector2, Vector3 和 Vector4 Rect Color和Color32 Bounds Touch 1.Class为引用类型,Str ...
- PowerShell 脚本中的密码
引言 笔者在<PowerShell 远程执行任务>一文中提到了在脚本中使用用户名和密码的基本方式: $Username = 'xxxx' $Password = 'yyyy' $Pass ...