android 中很多应用都是用ListView来显示数据
就像系统中的设置里面,每一行,就是构成的ListView
private ListView listview;
private PersonService person;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
person = new PersonService(this);
listview = (ListView)this.findViewById(R.id.listview);
show();
}
private void show(){
List<Person> list = person.getPageData(0, 20);
//以下使用适配器,来完成数据与控件的绑定
List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();
for(Person person :list){
HashMap<String,Object> item = new HashMap<String,Object>();
item.put("name", person.getName());
item.put("phone", person.getPhone());
item.put("id", person.getId());
data.add(item);
}
SimpleAdapter adapter =
new SimpleAdapter(this,data,R.layout.item,
new String[]{"name","phone","amount"},
new int[]{R.id.name,R.id.mobile,R.id.saveMoney});
listview.setAdapter(adapter);
}