一、SimpleAdapter
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String,String> map = new HashMap<String, String>();
map.put("name", "xilige");
map.put("age", "48");
list.add(map);
map = new HashMap<String, String>();
map.put("name", "wangwu");
map.put("age", "20");
list.add(map);
SimpleAdapter simple = new SimpleAdapter(
this,
list,
R.layout.list_item,
new String[]{"name","age"},
new int[]{R.id.list_view_name,R.id.list_view_age});
listview.setAdapter(simple);
注 :list_item.xml文件中 创建TextView(list_view_name)及 TextView(list_view_age)
二、ArrayAdapter
List<Person> list = new ArrayList<Person>();
listview.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list));
三、BaseAdapter
1、方式一
List<Person> list = new ArrayList<Person>();
listview.setAdapter(new MyAdapter());
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textview = null;
if(null != convertView){
textview = (TextView) convertView;
}else
{
textview = new TextView(MainActivity.this);
}
textview.setTextSize(18);
textview.setText(list.get(position).toString());
return textview;
}
}
2、方式二
List<Person> list = new ArrayList<Person>();
listview.setAdapter(new MyAdapter());
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater flater = MainActivity.this.getLayoutInflater();
View view = flater.inflate(R.layout.list_item, null);
TextView name = (TextView) view.findViewById(R.id.list_view_name);
TextView age = (TextView) view.findViewById(R.id.list_view_age);
Person p = list.get(position);
name.setText(p.getName());
age.setText(String.valueOf(p.getAge()));
return view;
}
}