SimpleAdapter

来源:互联网 发布:青岛seo外包公司 编辑:程序博客网 时间:2024/05/29 17:44

转自http://www.cnblogs.com/wenjiang/p/3189082.html 

使用ListView关键的就是适配器,可怕的是,用于ListView的适配器很多。

      我们先从名字看似最简单其实不简单的SimpleAdapter开始。

      我们先来看一个简单的例子:

复制代码
    private ListView mListView;    private LinearLayout mLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mLayout = new LinearLayout(this);        mLayout.setOrientation(LinearLayout.VERTICAL);        mListView = new ListView(this);        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(                LinearLayout.LayoutParams.MATCH_PARENT,                LinearLayout.LayoutParams.WRAP_CONTENT);        mLayout.addView(mListView, param);        setContentView(mLayout);        Map<String, String> keyValuePair = new HashMap<String, String>();        keyValuePair.put("Name", "小智");        keyValuePair.put("Age", "10");        List<Map<String, String>> list = new ArrayList<Map<String, String>>();        list.add(keyValuePair);        ListAdapter adapter = new SimpleAdapter(this, list,                android.R.layout.simple_list_item_2, new String[] { "Name",                        "Age" }, new int[] { android.R.id.text1,                        android.R.id.text2 });        mListView.setAdapter(adapter);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }
复制代码

     

     我们先从SimpleAdapter的构造开始。
     要构造一个SimpleAdapter,需要以下的参数:

1.Context context:上下文,这个是每个组件都需要的,它指明了SimpleAdapter关联的View的运行环境,也就是我们当前的Activity。

2.List<? extends Map<String, ?>> data:这是一个由Map组成的List,在该List中的每个条目对应ListView的一行,每一个Map中包含的就是所有在from参数中指定的key。

3.int resource:定义列表项的布局文件的资源ID,该资源文件至少应该包含在to参数中定义的ID。

4.String[] from:将被添加到Map映射上的key。

5.int[] to:将绑定数据的视图的ID跟from参数对应,这些被绑定的视图元素应该全是TextView。

    上面的例子中我们是手动的添加视图,然后使用的是系统默认的视图元素,像是android.R.id.text1。当然,我们也可以自定义TextView的样式,而且,说是应该全是TextView,也只是应该,并不是绝对的:

复制代码
@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ListView listView = (ListView) this.findViewById(R.id.list);        List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();        for (int i = 0; i < 5; i++) {            Map<String, String> keyValuePair = new HashMap<String, String>();            keyValuePair.put("Text", "Text" + i);            keyValuePair.put("Button", "Button" + i);            list.add(keyValuePair);        }        ListAdapter adapter = new SimpleAdapter(this, list, R.layout.listitem,                new String[] { "Text", "Button" }, new int[] { R.id.text,                        R.id.button });        listView.setAdapter(adapter);
复制代码

       

      从这里我们可以看到,要想使用ListView,我们应用程序的主界面必须包含ListView,然后ListView的内容可以自己定义,而不仅仅是TextView。

      要想知道这是什么回事,我们就要知道SimpleAdapter是如何绑定数据到视图的,这个过程我们甚至可以自定义:

复制代码
@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ListView listView = (ListView) this.findViewById(R.id.list);        List<Map<String, String>> list = new ArrayList<Map<String, String>>();        for (int i = 0; i < 3; i++) {            Map<String, String> keyValuePair = new HashMap<String, String>();            keyValuePair.put("text", "text" + i);            list.add(keyValuePair);        }        CustomSimpleAdapter adapter = new CustomSimpleAdapter(this, list,                R.layout.listitem);        listView.setAdapter(adapter);
复制代码

 

复制代码
class CustomSimpleAdapter extends SimpleAdapter {        private int mResource;        private List<? extends Map<String, ?>> mData;        public CustomSimpleAdapter(Context context,                List<? extends Map<String, ?>> data, int resource) {            super(context, data, resource, null, null);            this.mResource = resource;            this.mData = data;        }        @Override        public View getView(int position, View convertView, ViewGroup group) {            LayoutInflater layoutInflater = getLayoutInflater();            View view = layoutInflater.inflate(mResource, null);            TextView text = (TextView) view.findViewById(R.id.text);            text.setText(mData.get(position).get("text").toString());            if (position == 2) {                text.setTextColor(Color.RED);            }            return view;        }    }
复制代码

     

      要想实现自定义的ListView,最主要的是实现getView(),因为SimpleAdapter的数据绑定就是发生在这里。
      现在我们可以总结一下SimpleAdapter的数据绑定是怎样的:利用传入的view(该view包含ListView每行要渲染的视图元素)的ResourceID得到该view,然后通过每个vie所在的索引,也就是它们的行数,得到data中相应内容的key,接着就是利用这些key的value填充这些视图元素,最后返回view作为ListView每行的内容显示出来。

      由此可见,from和to并不是必须的,要想实现ListView,前三个参数才是必要的,也许大家会看到网上有些例子为了实现自定义的SimpleAdapter,会覆写它的许多方法,其实如果单纯只是想要利用SimpleAdapter来实现自定义的ListView,只要覆写getView()就行,其他的完全可以交给SimpleAdapter原先的方法来做,除非我们有特殊的要求。

      SimpleAdapter并不仅仅用在ListView上,事实上,Spinner同样可以使用:

复制代码
@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Spinner spinner = (Spinner) this.findViewById(R.id.spinner);        List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();        for (int i = 0; i < 5; i++) {            Map<String, String> keyValuePair = new HashMap<String, String>();            keyValuePair.put("Text", "Text" + i);            list.add(keyValuePair);        }        SimpleAdapter adapter = new SimpleAdapter(this, list,                R.layout.listitem, new String[] { "Text" },                new int[] { R.id.text });        spinner.setAdapter(adapter);    }
复制代码

      

       因为Spinner显示的列表本质上就是一个ListView,所以,和ListView有关的一切它几乎都可以使用,这个还是放在Spinner那时候再讲吧。
      原本只是想要将所有的适配器讲完,但碍于篇幅有限,所以只好分开讲,还有,SimpleCursorAdapter由于涉及到数据库,所以打算单独拿出来讲。

0 0