android 中的listview是最常用的控件之一。listview 显示数据,需要使用adapter进行适配。常用的adapter有Arrayadapter,simpleAdapter ,和BaseAdapter 。
Arrayadapter 主要用于显示只有文字的,simpleAdapter 和BaseAdapter 可以用来显示文字,图片 以及在list的每个item中添加 其他的控件如Button,Checkbox 并对其作出监听。
Arrayadapter 的效果图:
下面是Arrayadapter中的代码
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.arrayadapter); lv = (ListView) findViewById(R.id.lvArrayAdapter); lv.setAdapter(new ArrayAdapter<String >(this, android.R.layout.simple_list_item_1, getDatas())); //this,表示上下文,android.R.layout.simple_list_item1,表示行布局文件,这里调用的是系统自带的,也可以使用自定义的; //getDatas(),返回的是填充到adapter中的数据。 } private ArrayList<String> getDatas(){ ArrayList<String> datas = new ArrayList<String>(); datas.add("test1"); datas.add("test2"); datas.add("test3"); datas.add("test4"); datas.add("test5"); datas.add("test6"); return datas; }
simpleAdapeter 的效果图:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_adapter); lv = (ListView)findViewById(R.id.lvsimple); SimpleAdapter adapter = new SimpleAdapter( this,//this 表示上下文, getData(),//adapter 中填充的数据 R.layout.simple_item,//list 中自定义的行布局文件 new String[] {"img","name","age"},//key 值 //value 值,必须与key 值相对应 new int[]{R.id.imgSimpleItemImg,R.id.tvSimpleItemName,R.id.tvSimpleItemAge}); lv.setAdapter(adapter); } public List<Map<String, Object>> getData(){ List<Map<String ,Object>> list = new ArrayList<Map<String,Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "imesong1"); map.put("age","21"); map.put("img", R.drawable.img1); list.add(map); map = new HashMap<String, Object>(); map.put("name", "imesong2"); map.put("age","22"); map.put("img", R.drawable.img2); list.add(map); map = new HashMap<String, Object>(); map.put("name", "imesong3"); map.put("age","23"); map.put("img", R.drawable.img3); list.add(map); return list; }
R.layout.simple_item 自定义的布局文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imgSimpleItemImg" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:layout_width="30dp" android:layout_height="match_parent" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tvSimpleItemName" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvSimpleItemAge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
baseAdapter 的效果图:
public class myAdapter extends BaseAdapter { private Context context; private LayoutInflater minflater; private ArrayList<Person> datas = new ArrayList<Person>(); public myAdapter(Context context, ArrayList<Person> datas) { super(); this.context = context; this.datas = datas; this.minflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return datas.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return datas.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder = null; if(convertView == null){//先判断convertView是否为空 holder = new Holder(); convertView = minflater.inflate(R.layout.base_item, null);//使用自定义的行布局文件 //实例化控件 holder.img = (ImageView) convertView.findViewById(R.id.imgBaseItemImg); holder.name = (TextView) convertView.findViewById(R.id.tvBaseItemName); holder.age = (TextView) convertView.findViewById(R.id.tvBaseItemAge); convertView.setTag(holder); }else { holder = (Holder) convertView.getTag(); }//为控件赋值 holder.img.setBackgroundResource(datas.get(position).getImg()); holder.name.setText(datas.get(position).getName()); holder.age.setText(datas.get(position).getAge()+"");//年龄为int 类型,转换 为String,不然会报bug return convertView; } class Holder { ImageView img; TextView name; TextView age; } }
public class myBaseAdapter extends Activity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.base_adapter); ArrayList<Person> datas = new ArrayList<Person>(); datas.add(new Person("imesong1",21,R.drawable.img1)); datas.add(new Person("imesong2", 22, R.drawable.img2)); datas.add(new Person("imesong3", 23, R.drawable.img3)); lv = (ListView) findViewById(R.id.lvbase); lv.setAdapter(new myAdapter(this, datas)); } //表示个人信息的内部类 class Person { String name; int age; int img; public Person(String name, int age, int img) { this.name = name; this.age = age; this.img = img; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getImg() { return img; } public void setImg(int img) { this.img = img; } } }myAdapter 中自定义的行布局文件 代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imgBaseItemImg" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:layout_width="30dp" android:layout_height="match_parent" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tvBaseItemName" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:layout_width="30dp" android:layout_height="match_parent" /> <TextView android:id="@+id/tvBaseItemAge" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>