android中的ListView适配器
ListView的数据适配器有arrayAdapter,SimpleAdapter
作用:数据适配器是把复杂的数据填充在视图界面上, 数据适配器是连接数据源和视图直接的桥梁
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
第一个参数Content context表示上下文, 暂时不知道用来干嘛
第二个参数List<? extends Map<string,?>>data ,list中的每一个map的内容对应一个ListView的一个item的内容,那么编译器如何知道map中的哪个内容显示在item的哪个位置呢
这就由下面的三个参数来决定
第三个参数int reresource表示ListView的每一个item的布局文件
第四个参数String[] from, 这个参数限定了map中应该使用的键是什么
第五个参数int[] to, 这个参数和from向对应,即表示要用哪个键在map中对应的内容替换to所指向的内容。
simpleAdapter = new SimpleAdapter(this,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text})
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#584954" android:text="demo"/> </LinearLayout>
该布局文件表示了ListView的每个item都有一张图片和一段文字
String[]{"pic","text"} 表示map中的键只能是"pic" , "text",
然后对于ListView的每一项,从list中找到相对应的map,然后用Stirng[]相应的键,找到相应的值,然后这这些数据显示到to[]相对于的控件去。