博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Adapter适配器
阅读量:4036 次
发布时间:2019-05-24

本文共 3994 字,大约阅读时间需要 13 分钟。

Android Adapter适配器
Adapter的作用就是ListView界面与数据之间的桥梁,当列表里的每一项显示到页面时,都会调用Adapter的getView方法返回一个View。
Android中有很多的适配器,首先看看这些适配器的继承结构
Data、Adapter、View三者的关系
  
Data、Adapter、View三者的关系
一个listAdapter用来管理一个用一组任意对象的数组填充的ListView。
ListAdapter列表项的显示配置:
一、
通过简单XML文件配置
默认的ListAdapter希望提供的ListView每一项的 xml布局配置文件中只有一个TextView。
构造函数:public ArrayAdapter (Context context, int textViewResourceId)
  context: 当前的上下文对象
  textViewResourceId: The resource ID for a layout file containing a TextView to use when instantiating views. 一个包含了TextView的布局xml文件的id,注意(这个布局文件里只能有TextView一个控件,TextView不能有父控件,否则会报错 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView)
  类似于这种的xml
  <?xml version="1.0" encoding="utf-8"?>
  <TextView android:id="@+id/subject"
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:layout_marginTop="5dip" android:textAppearance="?android:attr/textAppearanceMedium"
   android:singleLine="true" android:ellipsize="end" />
 public ArrayAdapter (Context context, int textViewResourceId, T[] objects)
  objects:用来填充ListView,给ArrayAdapter提供数据的数组
public ArrayAdapter (Context context, int textViewResourceId, List<T> objects) 
//建议使用这个,直接给ArrayAdapter填充了数据
二、
复合布局文件XML配置
如果你想使用一个复合布局的话,你就要使用含有id字段的构造函数了,这个id要去引用这个复杂布局文件 中的一个TextView,TextView被引用了,使用数组中的对象,调用toString方法,转换成字符串来填充这个TextView,你可以使 用包含自定义对象的数组或者集合。重写自定义对象的toString()方法,来保证ListView显示。
public ArrayAdapter (Context context, int resource, int textViewResourceId)
这个是用来复杂布局的,ListView的Item项的布局文件中不止含有一个TextView控件
  resource: The resource ID for a layout file containing a layout to use when instantiating views. ListView中Item项的复杂布局xml文件
  textViewResourceId:The id of the TextView within the layout resource to be populated(显示) ListView中Item项的复杂布局xml文件中用来显示ArrayAdapter中数据的那个TextView
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects)
//建议使用这个,直接给ArrayAdapter填充了数据。
  方法:
  这个方法能够使用数组xml文件中配置的数据来创建一个ArrayAdapter,这个数组中的内容如何获得,通过this.getResources().getTextArray(id)方法获得。
  自定义数组xml文件的标识id号,也就是ArrayAdapter要绑定到ListVIew中的数据
textViewResourceId:用于显示数组数据的布局文件的id标识号(注意:该布局文件中只能有一个TextView,有多个就会报错,一般是 ClassCastException)
三、
通过重写Adapter的getView方法来得到你想要的view。
你也可以是使用其他的一些非TextView 控件来显示数组中的数据,通过重写Adapter的getView方法来得到你想要的view。
public View getView(int position, View convertView, ViewGroup parent) { 
ViewHolder holder; 
if (convertView == null) { 
convertView = mInflater.inflate(R.layout.list_item_icon_text, null); 
holder = new ViewHolder(); 
holder.text = (TextView) convertView.findViewById(R.id.text); 
holder.icon = (ImageView) convertView.findViewById(R.id.icon); 
convertView.setTag(holder); 
} else { 
holder = (ViewHolder) convertView.getTag(); 
holder.text.setText(DATA[position]); 
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2); 
return convertView; 
}
static class ViewHolder { 
TextView text; 
ImageView icon; 
}
BaseAdapter是一个抽象类,继承它需要实现较多的方法,所以也就具有较高的灵活性
构造一个Adapter:
public class xxxxAdapter extends BaseAdapter { 
private Context context; 
private List<xxxx> xxxxList; 
public xxxxAdapter(Context context, List<xxxx> weatherList ) { 
this.context = context; 
this.xxxxrList = xxxxList; 
//1.先判断adapter 有多少数据项,根据这个数据确定有多少item.
public int getCount() { 
return xxxxList!=null? xxxxList.size() :0; 
public Object getItem(int position) { 
return xxxxList.get(position); 
public long getItemId(int position) { 
return position; 
//2 确定每个item里加载哪个View
public View getView(int position, View convertView, ViewGroup parent) { 
参考上面
参考:
1.http://www.cnblogs.com/tanlon/archive/2011/05/21/2053009.html
2. Android常见控件之SimpleAdapter和List
http://www.bianceng.cn/OS/extra/201106/27059.htm
3. Cursor与Adapter结合使用http://hi.baidu.com/lfcaolibin/blog/item/2ce306ec77cebd4478f055b4.html
4. android 适配器Adpter的使用总结 之 CursorAdpter
http://www.cnblogs.com/tanlon/archive/2011/06/20/2085562.html
你可能感兴趣的文章
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>
java swing最简单实例(2) 往JFrame里面放一个容器或组件
查看>>
java自定义容器排序的两种方法
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
从头开始学习JSP(3)——一些配置
查看>>
html常用标签快速检索
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>
DIV/CSS:一个贴在左上角的标签
查看>>
通过/proc/PID/status查看进程内存占用情况
查看>>
/proc文件系统读出来的数据是最新的吗?
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
Vue组件
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>