![ListView之SimpleAdapter列表 ListView之SimpleAdapter列表](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
![ListView之SimpleAdapter列表 ListView之SimpleAdapter列表](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
新建一个datalist.xml文件在我们的layout下边
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
</TableLayout>
我们的主要的xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
</RelativeLayout>
我们的.java程序
public class MainActivity extends Activity {
private String data[][]={
{"001","谷歌"},
{"002","苹果"},
{"003","微软"},
{"004","IBM"},
{"005","亚马逊"},
{"006","甲骨文"},
{"007","三星"},
{"008","联想"},
{"009","索尼"},
{"010","HTC"},
{"011","诺基亚"},
{"012","摩托罗拉"},
{"013","百度"},
{"014","华为"},
{"015","中兴"}
};
private ListView datalist=null;
//首先定义一个List集合,该集合当中是Map类型
private List<Map<String,String>> list=new ArrayList<Map<String,String>>();
//进行数据的转换操作
private SimpleAdapter simpleAdapter=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//取得组件(我们定义在datalist.xml当中的组件)
this.datalist=(ListView)super.findViewById(R.id.datalist);
//
for(int i=0;i<this.data.length;i++){
//定义Map集合,保存每一行数据
Map<String,String> map=new HashMap<String,String>();
map.put("_id", this.data[i][0]);
map.put("name", this.data[i][1]);
//添加到list当中
this.list.add(map);
//这样就将我们的二维数据转换成了List对象
}
//构造我们的SimpleAdapter适配器
this.simpleAdapter=new SimpleAdapter(
this,
this.list,R.layout.datalist,
new String[]{"_id","name"},//表示的是Map当中的key的名称
new int[]{R.id._id,R.id.name}//datalist.xml当中定义的组件的ID
);
this.datalist.setAdapter(this.simpleAdapter);
}
}