1.首先写一个Adapter项布局文件,主要用来设置listView每一项的布局
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.myapplication.ToastActivity"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="姓名:"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text1"
android:text="内容"
android:gravity="center"
android:layout_weight="0.3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="年龄:"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text2"
android:text="内容"
android:gravity="center"
android:layout_weight="0.3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="性别:"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text3"
android:text="内容"
android:gravity="center"
android:layout_weight="0.3"
/>
</LinearLayout>
效果:
-
2.编写我们的java代码
List<Map<String,String>> listMap=new ArrayList<Map<String,String>>();//数据源
Map<String,String> map1=new HashMap<String,String>();
map1.put("name","张三");
map1.put("age","20");
map1.put("sex", "男");
Map<String,String> map2=new HashMap<String,String>();
map2.put("name","小红");
map2.put("age", "20");
map2.put("sex", "女");
listMap.add(map1);
listMap.add(map2);SimpleAdapter simpleAdapter=new SimpleAdapter
(
this,//上下文
listMap,//数据源
R.layout.simple_adapter_item,//指向自定义的项布局文件
new String[]{"name","age","sex"},//key数组
new int[]{R.id.text1,R.id.text2,R.id.text3}//项布局文件里的控件数组
/**
* 简单理解为:从list<map>中获取每一个map元素,根据key获取内容放到指定的项布局文件中的指定控件,最后形成一项
* 后俩个数组参数的元素是是对应的
*/
);
listView.setAdapter(simpleAdapter);