![](https://i-blog.csdnimg.cn/blog_migrate/7a4d106c37f25796203bb90231537fb7.png)
<RelativeLayout 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.test15.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
package com.example.test15;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ActionBarActivity {
private ListView listView;
private List<Map<String, String>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillDate();
listView=(ListView) findViewById(R.id.listView1);
//创建适配器
SimpleAdapter simpleAdapter=new SimpleAdapter(
this, //山下文
data, //数据源
R.layout.list_item, //项目布局
new String[]{"name","age"}, //数据源中的key
new int[]{R.id.tvName,R.id.tvAge});//显示数据的控件id
listView.setAdapter(simpleAdapter);
}
//填充数据
private void fillDate(){
data=new ArrayList<Map<String,String>>();
//第一条数据
Map<String,String> stu1=new HashMap<String, String>();
stu1.put("name", "张三");
stu1.put("age", "20");
data.add(stu1);
//第二条数据
Map<String, String> stu2=new HashMap<String, String>();
stu2.put("name", "李四");
stu2.put("age", "25");
data.add(stu2);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tvName"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>