CustomAdapter -> 自定义适配器
安卓自带的适配器不会面面俱到,有一些需要的样式需要自己写,适配器也需要自己写
同样是三步:
这次的适配器不再用系统自带的了,而是自己写
-
适配器需要继承自BaseAdapter
-
需要重写里面的几个方法
-
传递的参数一般为环境上下文,数据以及布局资源
需要布局资源,所以需要自己写一个布局文件,相当于QQ列表每个好友为一个元素,布局文件的布局为一个元素的布局。(注意一个!!)
动态更新Adapter数据的方法:
Adapter.notifyDataSetChanged()
重要代码:适配器getView方法中的代码
用来获取布局填充器的:
LayoutInflater inflater = LayoutInflater.from(mContext);//布局填充器
convertView = inflater.inflate(itemLayoutRes, null);
主页面用来显示,显示在ListView上
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<ListView
android:id="@+id/lv_student"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
androi