Android适配器及其控件
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控件中的内容,通过数组适配器实现了这一个目的。
另附一篇好文:简单好用的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 List
三、BaseAdapter
BaseAdapter是最基础的Adapter类,也是最实用最常用的一个类,但是相比于ArrayAdapter之类的,对初学者来说却比较难理解。内容较多,所以在下面引用几篇文章:
BaseAdapter使用教程及方法详解
Android必学之数据适配器BaseAdapter
Android应用项目中BaseAdapter、SimpleAdapter和ArrayAdapter中的三种适配器
更多相关文章
- Android基本界面控件
- 控件(2)toast
- Android在XML中自定义控件的使用
- Android Ap 开发 设计模式第二篇:适配器模式
- Android06_Android中常用控件
- C# android base-64 字符数组的无效长度
- 软键盘默认不弹出,点击别的控件让EditText获得焦点并弹出软键盘
- Android基本控件和事件以及消息总结
- android中控件点击两次才响应onclick方法