SimpleAdapter适配器构造函数的解析:
1--构造函数的原型:
SimpleAdapter(Context context,List<? extends Map<String,?>>data,int resource,String[] from,int[] to)
2--构造函数四大参数的描述:
1--Context context---SimpleAdapter适配器类型所在的Activity界面表现组件的实例化对象
2--List<? extends Map<String,?>>data----data参数就是我们需要加载到ListView列表控件中的数据,每一个data
参数的具体实例,对应ListView列表控件中的每一项列表项所对应的所要显示ide具体数据
3--int resource---ListView的每一个列表项指的就是resource参数指定的布局
比如说,你在使用SimpleAdapter适配器作为ListView列表控件的适配器的应用中,定义了三个文件:
1--MainActivity.java-----显示界面布局的主Activity界面表现文件
2--main_activity.xml-----主界面布局文件
3--user.xml--------------自定义的,对应于ListView列表控件中每一个列表项的---布局文件
3--那么,int resource---指的就是---R.layout.user
4--String[] from参数
比如说,你使用ArrayList集合中的每一个元素对象---对应存储ListView列表控件中的每一个项的---数据,代码如下
ArrayList<HashMap<String,Object>> users=new ArrayList<HashMap<String,Object>>();
然后使用循环,项ArrayList集合中添加数据,由于HashMap也是一个集合,所以每一个HaspMap的对象可以存储每一
个ListView中每一个具体想的数据
比如说:每一个ListView列表控件的每一项有三部分内容,分别要显示:
1---图标/图片
2---姓名
3---年龄
那么,我们可以通过如下的代码实现:、
//【1】向ArrayList集合中,添加10项数据,也就是ListView列表控件将显示10项数据:
for(int i=0;i<10;i++){
HashMap<String,Object> user=new HashMap<String,Object>();
//【2】向列表项中的每一具体部分添加数据---图标,姓名,年龄
user.put("img",R.drawable.img1);
user.put("username","姓名("+i+")");\
user.put("userage",i+11);
//【3】向ArrayList集合中添加数据
users.add(user);
}
from参数值-----得就是这块HashMap集合中的键值---key
5--int[] to--这块的to指的就是界面布局显示相应数据控件的id值:
比如说,我们自定义的显示ListView列表控件中,每一个列表项的user.xml文件如下:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/img"
/>
<TextView
android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"
/>
<TextView
android:id="@+id/txtAge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#000000"
/>
那么,参数to指的就是:
new int[]{R.id.img,R.id.txtName,R.id.txtAge}