android高级组件(3)适配器 ArrayAdapter,SimpleAdapter,BaseAdapter

来源:互联网 发布:淘宝摄影后期质感 编辑:程序博客网 时间:2024/06/08 08:18

适配器

适配器简单点说,就是把数据源中的每一个元素按某种布局方式垂直排列,说到底还是一个布局所有需要适配器的高级组件,都有一个 setAdapter的方法,所以适配器一般配合setAdapter使用

ArrayAdapter

ArrayAdapter 只能关联一个组件///自定义一个ArrayAdapter    ArrayAdapter<T> adapter = new ArrayAdapter<T>(this,R.layout.autoadaptor, R.id.tv1, names);//this :上下文//R.layout.autoadaptor : 数据源要使用的布局//R.id.tv1 : 添加到布局的哪个组件//names :数据源
//系统ArrayAdapter    ArrayAdapter<T> adapter = new ArrayAdapter<T>(this,android.R.layout.simple_list_item_1, names);

SimpleAdapter

数据源:List<HashMap<String , Object>>构造方法:SimpleAdapter adapter = new SimpleAdapter(this , getData() , R.layout.item_person , from , to);//存放的是在布局中所有的需要设置值的组件的idint[] to = {R.id.img};//在HashMap中,所有的键的值String[] from = {"img"};//1对1 映射关系 两个数组的每一位都是关联在一起的List的长度是条目的个数HashMap 是每个条目的数据
在Activity_main中创建ListView组件    <ListView         android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </ListView>//新建一个xml模版muban.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:padding="10dp" >    <ImageView        android:id="@+id/img"        android:layout_width="150dp"        android:layout_height="150dp"        android:adjustViewBounds="true"        android:src="@drawable/zhaoyun" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <TextView            android:id="@+id/name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:padding="5dp"            android:singleLine="true"            android:text="赵云"            android:textSize="24sp" />        <TextView            android:id="@+id/info"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:padding="5dp"            android:text="赵云(?-229年),字子龙,常山真定(今河北省正定)人。身长八尺,姿颜雄伟,三国时期蜀汉名将。" />    </LinearLayout></LinearLayout>//在MainActivity中创建SimpleAdapterSimpleAdapter adapter = new SimpleAdapter(this , list , muban.xml , from , to);//创建list , from , to//from:key值数组(String类型),to:布局空间id数组(int类型))List<HashMap<String , Object>> list = new List<HashMap<String , Object>>();String[] key = {"img" , "name" ,"info"}; //存放的是布局中所有需要设置的组件int[] view = {"R.id.img" , "R.id.name" , "R.id.info"}; public List<HashMap<String , Object>> getData(){//HashMap 中有多少个key,说明需要设置在一个条目中视图的个数    HashMap<String , Object> zhaoyun = new HashMap<String , Object>();    zhaoyun.put("img" , R.drawable/zhaoyun);    zhaoyun.put("name" , "赵云");    zhaoyun.put("info" , "赵云(?-229年),字子龙,常山真定(今河北省正定)人。身长八尺,姿颜雄伟,三国时期蜀汉名将。");    list.add(zhaoyun);    HashMap<String , Object> machao = new HashMap<String , Object>();    machao.put("img" , R.drawable/machao);    machao.put("name" , "马超");    machao.put("info" , "马超(176年-222年),字孟起,扶风茂陵人,汉伏波将军马援的后人,马腾的儿子,少年成名。");    list.add(machao);    return list;}

BaseAdapter 最常用的适配器List

万能适配器,什么都可以做

1.在Activity给一个ListView    <ListView         android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="match_parent" />2.findViewById    ListView lv = (ListView) findViewById(R.id.lv);3.为item去做布局muban.xml4.加载数据    class User{        int imgId;        String name;        String info;        public User(int imgId, String name, String info) {            super();            this.imgId = imgId;            this.name = name;            this.info = info;        }    }    List<User> users = new ArrayList<User>();        users.add(new User(R.drawable.zhaoyun, "赵云", "常山真定人"));        users.add(new User(R.drawable.machao, "马超", "扶风茂陵人"));        users.add(new User(R.drawable.zhouyu, "周瑜", "庐江舒人"));5.设计适配器    class MyBaseAdapter extends BaseAdapter {        @Override        public int getCount() {        // 整个数据的长度            return users.size();        }        @Override        public Object getItem(int position) {        // 获取当前的item            return position;        }        @Override        public long getItemId(int position) {        // 返回position            return 0;        }        // 每一条数据对应的布局        @Override        public View getView(int position, View convertView, ViewGroup parent) {        // position:当前整个布局是 第几条item            View layout = View.inflate(getBaseContext(), R.layout.list, null);            ImageView img = (ImageView) layout.findViewById(R.id.img);            TextView name = (TextView) layout.findViewById(R.id.name);            TextView info = (TextView) layout.findViewById(R.id.info);            User user = users.get(position);            img.setImageResource(user.imgId);            name.setText(user.name);            info.setText(user.info);            return layout;        }    }6.给ListView设置适配器lv.setAdapter(new MyBaseAdapter());//布局重用
阅读全文
0 0
原创粉丝点击