main.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" />
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="56dp"
>
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="48dp"
android:layout_height="48dp"
/>
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_gravity="center_vertical"
android:stretchColumns="0,1"
>
<TableRow >
<TextView android:id="@+id/tv_01"/>
<TextView android:id="@+id/tv_02"/>
</TableRow>
<TableRow >
<TextView android:id="@+id/tv_03"/>
<TextView android:id="@+id/tv_04"/>
</TableRow>
</TableLayout>
</LinearLayout>
activity
package com.ghg.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class Day0604_SimpleAdapterActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
//构造数据源,一个存放map集合的ArrayList
ArrayList<HashMap<String, Object>> list;
private ArrayList<HashMap<String, Object>> getList(){
list=new ArrayList<HashMap<String,Object>>();
for(int i=1;i<=20;i++){
HashMap<String, Object> map=new HashMap<String, Object>();
//姓名,身高(cm),体重(斤),价格(万)
map.put("name", "jiajia"+i);
map.put("height", 160+new Random().nextInt(10));
map.put("weight", 100+new Random().nextInt(10));
map.put("price", 100+new Random().nextInt(200));
list.add(map);
}
return list;
}
ListView listView;
private void initView() {
// TODO Auto-generated method stub
listView=(ListView) findViewById(R.id.listView);
String[] from={"name","height","weight","price"};
int[] to={R.id.tv_01,R.id.tv_02,R.id.tv_03,R.id.tv_04};
SimpleAdapter adapter=new SimpleAdapter(this, getList(), R.layout.item, from, to);
listView.setAdapter(adapter);
}
}