Android中关于Adapter的使用(中)SimpleAdapter
2014-02-15 17:46
381 查看
在前面的两篇文章中,我们讲到了关于ArrayAdapter的使用。用ArrayAdapter来在ListView中展示数据是很不错的,但是很多时候,我们的ListView中,可不只是展示文字,我们还想展示图片呢。
可能有些朋友刚才会问,第二篇不是已经可以展示图片了吗?是的呀,但是它就只能展示我们在xml中定义给它的那一张啊。
而究其原因,其实是因为我们传给它的数据源就只有字符串,没有传给图片给它,而事实上,我们也只能传文字给它,因为它用的是TextView嘛。
所以,光用ArrayAdapter显然不够丰富多彩,生活呀,总得有点不一样吧。
接下来我们就来看看在Android中关于SimpleAdapter的使用吧。
接下来我们看看MainActivity中的代码:
从上面的代码中,我们可以看到,跟ArrayAdapter类似,也要先创建一个SimpleAdapter,其构造函数如下:
2)List<? extends Map<String, ?>> data,这是一个包含Map对象的list,也就是说,每个map对象都是每个item对应的内容。
3)resource,对应的就是这个item的layout,也就是我们上面自定义的布局。
4)from 和 to,这两个参数,其实就是做一个映射,将map中key对应的值,传给layout中每个对象的id。比如,我们上面在map中设置了title,那么这个title的值就是设给item布局对应的TextView(tvTitle),而其它的事情就由SimpleAdapter来帮我们操作了。
下面就是效果图(有点丑,请不要见怪,@-@!!):
虽然说SimpleAdapter一点也不simple,但是如果我们理解了,用起来是不是也是挺简单的呢?
大家如果对ArrayAdapter有兴趣的话,可以看看前面关于ArrayAdapter的文章:
Android中关于Adapter的使用(上)ArrayAdapter
Android中关于Adapter的使用(再上)ArrayAdapter
可能有些朋友刚才会问,第二篇不是已经可以展示图片了吗?是的呀,但是它就只能展示我们在xml中定义给它的那一张啊。
而究其原因,其实是因为我们传给它的数据源就只有字符串,没有传给图片给它,而事实上,我们也只能传文字给它,因为它用的是TextView嘛。
所以,光用ArrayAdapter显然不够丰富多彩,生活呀,总得有点不一样吧。
接下来我们就来看看在Android中关于SimpleAdapter的使用吧。
SimpleAdapter
SimpleAdapter其实一点也不simple的,因为我们需要自己去定义每个listitem的布局,如下:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dip" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView1" android:layout_width="80dip" android:layout_height="60dip" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:contentDescription="testing"/> <TextView android:id="@+id/tvTitle" android:layout_width="80dip" android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageView1" android:layout_alignTop="@+id/imageView1"/> <TextView android:id="@+id/tvContent" android:layout_width="80dip" android:layout_height="wrap_content" android:layout_below="@+id/tvTitle" android:layout_toRightOf="@+id/imageView1" android:layout_alignBottom="@+id/imageView1"/> </RelativeLayout>我们定义了1个ImageView控件和两个TextView控件。
接下来我们看看MainActivity中的代码:
List<Map<String, Object>> simpleList = new ArrayList<Map<String, Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initSimpleList(); ListView listView = (ListView)findViewById(R.id.listView1); SimpleAdapter adapter = new SimpleAdapter(this, simpleList, R.layout.simpleadapter, new String[] {"title","content","drawable"}, new int[] {R.id.tvTitle,R.id.tvContent,R.id.imageView1}); listView.setAdapter(adapter); } private void initSimpleList(){ String[] titles = {"title1", "title2", "title3","title4", "title5", "title6"}; String[] contents = {"content1", "content2", "content3","content4", "content5", "content6"}; int[] drawableIds = {R.drawable.computer,R.drawable.excel,R.drawable.game, R.drawable.gc,R.drawable.network,R.drawable.pdf}; Map<String, Object> map; for(int i=0;i<6;i++){ map = new HashMap<String, Object>(); map.put("title", titles[i]); map.put("content", contents[i]); map.put("drawable", drawableIds[i]); simpleList.add(map); } }
从上面的代码中,我们可以看到,跟ArrayAdapter类似,也要先创建一个SimpleAdapter,其构造函数如下:
/** * Constructor * * @param context The context where the View associated with this SimpleAdapter is running * @param data A List of Maps. Each entry in the List corresponds to one row in the list. The * Maps contain the data for each row, and should include all the entries specified in * "from" * @param resource Resource identifier of a view layout that defines the views for this list * item. The layout file should include at least those named views defined in "to" * @param from A list of column names that will be added to the Map associated with each * item. * @param to The views that should display column in the "from" parameter. These should all be * TextViews. The first N views in this list are given the values of the first N columns * in the from parameter. */ public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { mData = data; mResource = mDropDownResource = resource; mFrom = from; mTo = to; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }1)context,上下文
2)List<? extends Map<String, ?>> data,这是一个包含Map对象的list,也就是说,每个map对象都是每个item对应的内容。
3)resource,对应的就是这个item的layout,也就是我们上面自定义的布局。
4)from 和 to,这两个参数,其实就是做一个映射,将map中key对应的值,传给layout中每个对象的id。比如,我们上面在map中设置了title,那么这个title的值就是设给item布局对应的TextView(tvTitle),而其它的事情就由SimpleAdapter来帮我们操作了。
下面就是效果图(有点丑,请不要见怪,@-@!!):
虽然说SimpleAdapter一点也不simple,但是如果我们理解了,用起来是不是也是挺简单的呢?
大家如果对ArrayAdapter有兴趣的话,可以看看前面关于ArrayAdapter的文章:
Android中关于Adapter的使用(上)ArrayAdapter
Android中关于Adapter的使用(再上)ArrayAdapter
相关文章推荐
- Android中关于Adapter的使用(中)SimpleAdapter
- Android中关于Adapter的使用(中)SimpleAdapter
- Android中关于Adapter的使用(上)ArrayAdapter
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- Android实例demo11之listview的简单使用(arrayadapter、simpleadapter)
- Android中关于Adapter的使用(上)ArrayAdapter
- Android开发 -- 关于RecylerView的使用和RecylerViewAdapter的实现及监听
- 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- [Android初级]关于Adapter使用notifyDataSetChanged无效的原因和解决方案
- 使用详解及源码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- Android学习---ListView的点击事件,simpleAdapter和arrayadapter的原理和使用 - Hi_Amos
- 使用详解及源码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- Android之ListView使用方法-SimpleAdapter-SimpleCursorAdapter
- Android中关于Adapter的使用(下)BaseAdapter
- Android中关于Adapter的使用(下)BaseAdapter
- Android中关于Adapter的使用(下)BaseAdapter
- Android中关于Adapter的使用(上)ArrayAdapter
- android ListView使用的三种方法汇总(通过继承BaseAdapter,使用SimpleAdapter和SimpleCursorAdapter)
- Android——关于PagerAdapter的使用方法的总结(转)
- Android中关于Adapter的使用(再上)ArrayAdapter