Android适配器

适配器是AdapterView视图(如ListView - 列表视图控件、Gallery - 缩略图浏览器控件、GridView - 网格控件、Spinner - 下拉列表控件、AutoCompleteTextView - 自动提示文本框、ExpandableListView - 支持展开/收缩功能的列表控件等)与数据之间的桥梁,用来处理数据并将数据绑定到AdapterView上。
android提供多种适配器,开发时可以针对数据源的不同采用最方便的适配器,也可以自定义适配器完成复杂功能。
Android中有三种适配器

  • ArrayAdapter
  • SimpleAdapter
  • BaseAdapter

一、ArrayAdapter

ArrayAdapter是BaseAdapter的派生类,在BaseAdapter的基础上,添加了一项重大的功能:可以直接使用泛型构造。

示范:
要求:使用数组适配器输出相对应的数据到ListView中
 1、首先、创建Android工程,后在布局文件中定义一个ListView,并且设置好相关属性:
 

<RelativeLayout 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" >    <ListView        android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>RelativeLayout>

简单好用的Adapter—ArrayAdapter
ArrayAdapter在ListView的详解实现
 2、其次、在activity类中书写代码,具体请看代码,并且代码上有相关的注释
 

package com.mqz.android_arrayadapter;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends Activity {      private ListView listView;    //定义ListView用来获取到,布局文件中的ListView控件        private String[] city = {"广州","深圳","北京","上海","香港","澳门","天津"} ;  //定义一个数组,作为数据源        private ArrayAdapter arrayAdapter;    //定义一个数组适配器对象        @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            listView = (ListView)findViewById(R.id.listView); //获取布局文件中的ListView控件对象        /*         * Context context,  上下文对象         * int resource,    items项显示的布局样式,一般是系统的布局文  android.R.layout.** (但是需要选择和ListView相适合的布局文件否则运行报错)         * String[] objects  数组对象(数据源)         *          * */        //创建数组适配器对象,并且通过参数设置类item项的布局样式和数据源                arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1, city);        //把数组适配器加载到ListView控件中        listView.setAdapter(arrayAdapter);    }}

3、运行结果:这样不需要我们自己定义TextView控件中的内容,通过数组适配器实现了这一个目的。
Android适配器及其控件_第1张图片
另附一篇好文:简单好用的Adapter—ArrayAdapter

二、SimpleAdapter

要求:把后台数据填充到页面,其中包括需要填充的有TextView和ImageView,也就是名字和图片显示到页面上。
1、首先,创建好android工程,并且在主布局文件中添加一个ListView控件

<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:orientation="horizontal"    tools:context="com.mqz.android_simpleadapter.MainActivity" >    <ListView         android:id="@+id/listView"        android:layout_width="match_parent"        android:layout_height="match_parent"        >ListView>LinearLayout>

2、其次,在Activity类中书写代码,其间附有注释,具体如下:

 ListView listView; 2     SimpleAdapter simpleAdapter; 3  4     @Override 5     protected void onCreate(Bundle savedInstanceState) { 6         super.onCreate(savedInstanceState); 7         setContentView(R.layout.activity_violet); 8         listView = (ListView) findViewById(R.id.listView); 9         ListString, Object>> data = new ArrayListString, Object>>();10         String[] persons = {"张老师", "陈老师", "李老师", "黄老师"};11         String[] phoneNums = {"18706218201", "18706214521", "13908761231", "18512390812"};12         for (int i = 0; i < persons.length; i++) {13             HashMap<String, Object> map = new HashMap<String, Object>();14             map.put("username", persons[i]);15             map.put("phoneNum", phoneNums[i]);16             //插入图片17             map.put("photo",R.drawable.cc);18             data.add(map);19         }20         //(context,data,resource,from,to)21         simpleAdapter = new SimpleAdapter(this, data, R.layout.list_item, new String[]{"username","photo", "phoneNum"}, new int[]{R.id.username,R.id.photo, R.id.phoneNum});22         listView.setAdapter(simpleAdapter);23     }

三、BaseAdapter

BaseAdapter是最基础的Adapter类,也是最实用最常用的一个类,但是相比于ArrayAdapter之类的,对初学者来说却比较难理解。内容较多,所以在下面引用几篇文章:
BaseAdapter使用教程及方法详解
Android必学之数据适配器BaseAdapter
Android应用项目中BaseAdapter、SimpleAdapter和ArrayAdapter中的三种适配器

更多相关文章

  1. Android基本界面控件
  2. 控件(2)toast
  3. Android在XML中自定义控件的使用
  4. Android Ap 开发 设计模式第二篇:适配器模式
  5. Android06_Android中常用控件
  6. C# android base-64 字符数组的无效长度
  7. 软键盘默认不弹出,点击别的控件让EditText获得焦点并弹出软键盘
  8. Android基本控件和事件以及消息总结
  9. android中控件点击两次才响应onclick方法

随机推荐

  1. Android学好Shape不再依赖美工
  2. Android仿人人客户端
  3. Android(安卓)开源网络框架(Android-Async
  4. 通读Android文档系列 THREE
  5. Android框架揭秘读书笔记
  6. Android控件显示和隐藏
  7. 《Android底层开发技术》学习总结
  8. Xamarin Mono For Android(安卓)4.6.0700
  9. Android(安卓)应用程序获得系统权限
  10. 教你如何开关Android的APN网络