Adapter是个什么角色呢?其实它的作用就是View界面和数据之间的桥梁。我们可以看作是界面数据绑定的一种理解,它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等。

常用的适配器有:

(1)ArrayAdapter;             数组作为数据源,填充的是ArrayAdapter

(2)SimpleAdapter;           List作为数据源,填充的是SimpleAdapter

(3)SimpleCursorAdapter; 数据来源一般都是数据库查询得到的Cursor

(4)自定义适配器;               为什么要定义自己的适配器呢?原因就在于,当我们想用一些其它的展现方式,或者是我们需要的,呈现方式,这是就得DIY了。首先我们定义一个类让它继承自BaseAdapter,再让它实现一里面所说的那几个方法。那么这个自定义适配器就算好了。

下面我们看个实例就明白了:

一 .ArrayAdapter

1.xml代码:

 <?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="fill_parent"
android:orientation="vertical" > <ListView
android:id="@+id/myArrayList"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout>

2.java代码:

 package com.example.arrayadapter;

 import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity {
private ListView mListView;
private ArrayList<String> mArrayList = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.myArrayList);
mListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1, getData()));
} private ArrayList<String> getData() {
mArrayList = new ArrayList<String>();
mArrayList.add("item1");
mArrayList.add("item2");
mArrayList.add("item3");
mArrayList.add("item4");
mArrayList.add("item5");
mArrayList.add("item6");
mArrayList.add("item7");
mArrayList.add("item8");
mArrayList.add("item9");
mArrayList.add("item10");
return mArrayList;
}
}

3.效果图:

二.SimpleAdapter
 simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。

1.XML代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" > <ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30px" /> <TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px" />
</LinearLayout> </LinearLayout>

2.java代码:

 package com.example.simpleadapter;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter; public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.simple, new String[] { "title", "content", "img" },
new int[] { R.id.title, R.id.content, R.id.img });
setListAdapter(adapter);
} private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "SimpleAdapter");
map.put("content",
"SimpleAdapter用法SimpleAdapter用法SimpleAdapter用法SimpleAdapter用法");
map.put("img", R.drawable.m4);
list.add(map); map = new HashMap<String, Object>();
map.put("title", "SimpleAdapter");
map.put("content",
"SimpleAdapter用法SimpleAdapter用法SimpleAdapter用法SimpleAdapter用法");
map.put("img", R.drawable.m4);
list.add(map); map = new HashMap<String, Object>();
map.put("title", "SimpleAdapter");
map.put("content",
"SimpleAdapter用法SimpleAdapter用法SimpleAdapter用法SimpleAdapter用法");
map.put("img", R.drawable.m4);
list.add(map); map = new HashMap<String, Object>();
map.put("title", "SimpleAdapter");
map.put("content",
"SimpleAdapter用法SimpleAdapter用法SimpleAdapter用法SimpleAdapter用法");
map.put("img", R.drawable.m4);
list.add(map); return list;
}
}

3.效果图:

由于写在一个页面感觉太长了,所以下一篇继续总结SimpleCursorAdapter和自定义适配器~~~~~~~~~

Android学习之适配器ArrayAdapter SimpleAdapter的更多相关文章

  1. Android学习总结——适配器

    适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件.AutoComplet ...

  2. Android 常用数据适配器ArrayAdapter

    接着上篇文章<Android 采用Layout Inflater创建一个View对象>,本文采用常用数据适配器ArrayAdapter 新建项目后,在layout文件夹下新建list_it ...

  3. Android学习之适配器SimpleCursorAdapter

    三.   SimpleCursorAdapter与SimpleAdapter用法相近.只是将List对象换成了Cursor对象.而且SimpleCursorAdapter类构造方法的第四个参数from ...

  4. android学习-Adapter适配器进阶

    参考资源 Android 快速开发系列 打造万能的ListView GridView 适配器 实现代码复用,争取打机**的时间. android4.4源码 target=android-19 一般自定 ...

  5. Android 常用数据适配器SimpleAdapter

    在<Android 常用数据适配器ArrayAdapter>中介绍了ArrayAdapter数据适配器.但是存在一个缺陷,那就是条目的图标都固定相同,要显示每个条目的图标都不相同,那么使用 ...

  6. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  7. 42.Android之ListView中ArrayAdapter简单学习

    今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...

  8. 13.Android-ListView使用、BaseAdapter/ArrayAdapter/SimpleAdapter适配器使用

    1.ListView ListView 是 Android 系统为我们提供的一种列表显示的一种控件,使用它可以用来显示我们常见的列表形式.继承自抽象类 AdapterView.继承图如下所示: 以微信 ...

  9. Android学习之Adapter(数据适配器)

    1.定义     数据适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - 下拉列表控件. ...

随机推荐

  1. WEB打印大全

    1.控制"纵打". 横打”和“页面的边距. (1)<script defer> function SetPrintSettings() {  // -- advance ...

  2. VIM下的可视模式的相关知识

    三种可视模式: v 激活面向字符的可视模式: V 激活面向行的可视模式: ctrl+v 激活面向列块的可视模式: 选择高亮区: 上面的 v 是可以与跳转指令 以及表示范围的指令组合使用的. 如:vl, ...

  3. Python操作SQLServer示例(转)

    转自:http://www.cnblogs.com/lrzy/p/4346781.html 本文主要是Python操作SQLServer示例,包括执行查询及更新操作(写入中文). 需要注意的是:读取数 ...

  4. 针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)

    错误: 针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_A ...

  5. C# IEnumerator的使用

    迭代器模式是设计模式中行为模式(behavioral pattern)的一个例子,他是一种简化对象间通讯的模式,也是一种非常容易理解和使用的模式.简单来说,迭代器模式使得你能够获取到序列中的所有元素而 ...

  6. 制作U盘启动安装CentOS Linux系统

    制作U盘启动安装CentOS Linux系统 (特为老男孩教育&&51CTO学院在线三期同学而发) 方法一:使用UltraISO,将u盘做成启动盘 文件-->打开-->选择 ...

  7. linux 上安裝lnmp

    1.確保有一台服務器可以正常運行 2.熟練知道一些基本的命令 3.這裡我以lnmp集成環境為例 https://lnmp.org/install.html 4.安裝大約30分鐘左右 5.安裝完畢,訪問 ...

  8. windows 找不到文件'igfxHK.exe'

    现象:开机时windows报:windows 找不到文件'igfxHK.exe' 解决办法:win+r  输入services.msc  进入服务管理, 找到服务名称为: Intel(R) HD Gr ...

  9. python unittest 3- 框架Nose

    当前python的测试框架主要有以下三个: 1)zope.testing 2)py.test 3)Nose Nose下载:https://github.com/nose-devs/nose 1.Nos ...

  10. QT QTransform与QMatrix 有啥区别?

    刚开始学习QT,我使用的是QT5.12进行开发,要不时地查阅QT的官方帮助文档~ 仔细阅读QT官方帮助QTransform类以及QMatrix类,发现两个类的作用描述一模一样(“The QTransf ...