Android数据适配器Adapter简介
1、简介
Adapter是用来帮助填充数据的中间桥梁,简单点说就是:将各种数据以合适的形式显示到view上,在常见的View(List View,Grid View)等地方都需要用到Adapter!
初次接触感觉和OC中TableView的cell功能一样!
继承关系:
- BaseAdapter:抽象类,实际开发中我们会继承这个类并且重写相关方法,用得最多的一个Adapter!
- ArrayAdapter:支持泛型操作,最简单的一个Adapter,只能展现一行文字~
- SimpleAdapter:同样具有良好扩展性的一个Adapter,可以自定义多种效果!
- SimpleCursorAdapter:用于显示简单文本类型的listView,一般在数据库那里会用到,不过有点过时, 不推荐使用!
2、简单实例
对应的Java文件:
- public class LoginActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- String[] strarr = {"111","111","111","111","111",};
- ArrayAdapter<String> arrayAdapter =new ArrayAdapter<String>(this,android.R.layout
- .simple_expandable_list_item_1,strarr);
- ListView listView = (ListView)findViewById(R.id.listview);
- listView.setAdapter(arrayAdapter);
- }
- }
ArrayAdapter有五种布局类型:
simple_expandable_list_item_1:
simple_expandable_list_item_2:
simple_list_item_checked:
simple_list_item_multiple_choice:
simple_list_item_single_choice:
每个item的xml布局文件:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="horizontal">
- <ImageView
- android:id="@+id/imgtou"
- android:layout_width="64dp"
- android:layout_height="64dp"
- android:baselineAlignBottom="true"
- android:paddingLeft="8dp" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="8dp"
- android:textColor="#1D1D1C"
- android:textSize="20sp" />
- <TextView
- android:id="@+id/says"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="8px"
- android:textColor="#B4B4B9"
- android:textSize="14sp" />
- </LinearLayout>
- </LinearLayout>
数据Java文件:
- public class LoginActivity extends AppCompatActivity {
- private String[] names = new String[]{"猪八戒","孙悟空","唐僧"};
- private String[] says = new String[]{"饿了","吃俺老孙一棒","紧箍咒"};
- private int[] images = new int[]{R.drawable.icon,R.drawable.icon,R.drawable.icon};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- List<Map<String,Object>> listitem = new ArrayList<Map<String,Object>>();
- for (int i=0;i<names.length;i++){
- Map<String, Object> showitem = new HashMap<String, Object>();
- showitem.put("icon",images[i]);
- showitem.put("name",names[i]);
- showitem.put("say",says[i]);
- listitem.add(showitem);
- }
- //public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {}
- SimpleAdapter simpleAdapter = new SimpleAdapter(getApplicationContext(),listitem ,R.layout
- .list_item,new String[]{"icon","name","say"},new int[]{R.id.imgtou, R.id.name, R
- .id.says});
- ListView listView = (ListView)findViewById(R.id.listview);
- listView.setAdapter(simpleAdapter);
- }
- }
Android数据适配器Adapter简介的更多相关文章
- Android数据适配器(Adapter)优化:使用高效的ViewHolder
原文链接:http://stackvoid.com/using-adapter-in-efficiency-way/ 在使用Listview或GridView的时候,往往须要自己定义数据适配器.一般都 ...
- Android 数据适配器
把复杂的数据(数组.链表.数据库.集合等)填充到指定的视图界面上. arrayAdapter(数组适配器): 用于绑定一些格式单一的数据,数据源:数据或者集合. private Li ...
- Android学习之Adapter(数据适配器)
1.定义 数据适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件. ...
- ListView和Adapter数据适配器的简单介绍
ListView 显示大量相同格式数据 常用属性: listSelector listView每项在选中.按下等不同状态时的Drawable divider ...
- Android必学之数据适配器BaseAdapter
什么是数据适配器? 下图展示了数据源.适配器.ListView等数据展示控件之间的关系.我们知道,数据源是各种各样的,而ListView所展示数据的格式则是有一定的要求的.数据适配器正是建立了数据源与 ...
- 浅析android适配器adapter中的那些坑
做项目中遇到的,折磨了我将近两天,今天把经验分享出来.让大家以后少走点弯路,好了.简单来说一下什么是android的适配器,怎样定义.怎样添加适配器的重用性.怎样去减少程序的耦合性 适配器顾名思义是用 ...
- Android 常用数据适配器SimpleAdapter
在<Android 常用数据适配器ArrayAdapter>中介绍了ArrayAdapter数据适配器.但是存在一个缺陷,那就是条目的图标都固定相同,要显示每个条目的图标都不相同,那么使用 ...
- Android 常用数据适配器ArrayAdapter
接着上篇文章<Android 采用Layout Inflater创建一个View对象>,本文采用常用数据适配器ArrayAdapter 新建项目后,在layout文件夹下新建list_it ...
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...
随机推荐
- 04E: Sub-process /usr/bin/dpkg returned an error code (1)
- java获得磁盘、网络实时I/O速率
最近项目中需要一个平台硬件资源的监控模块,当时采用了Sigar中api,但是做到针对磁盘和网络的实时I/O速率的时候发现Sigar并没有直接支持的接口.于是……它就诞生了.底层采用C++编写,通过ja ...
- VBA当中的时间日期函数
目前还没发现VBA中有直接的函数能够将完整的年月日时分秒的文本格式日期转换成日期型日期的,那只能使用间接实现的办法.用dateserial + timeserial的方法.因为dateserial和t ...
- anaconda新建环境
安装tensorflow等如下: https://blog.csdn.net/Gransand/article/details/80713810 修改默认打开目录如下: https://blog.cs ...
- delphi 第4课
try 语句;(正常)except 语句; (意外处理部分) end: 例子: begin sum:=; try n:=strtoint(edit1.Text); except showMessage ...
- QueryList 来做采集
示例代码 先来感受一下使用 QueryList 来做采集是什么样子. 1 采集百度搜索结果列表的标题和链接.大理石平台价格 采集代码: $data = QueryList::get('https:// ...
- thinkphp 模板继承
模板继承是一项更加灵活的模板布局方式,模板继承不同于模板布局,甚至来说,应该在模板布局的上层.模板继承其实并不难理解,就好比类的继承一样,模板也可以定义一个基础模板(或者是布局),并且其中定义相关的区 ...
- create table常用命令
CREATE TABLE students( stuID INTEGER NOT NULL , stuname ) not null, sex int NOT NULL ); CREATE TABLE ...
- H5 小代码(实时更新)
:before { content: ''; display: inline-block; vertical-align: middle; height: %; } ↑ 自适应垂直居中 .clear ...
- vue-grid-layout
vue-grid-layout vue-grid-layout is a grid layout system, like Gridster, for Vue.js. Heavily inspi ...