Android中关于Adapter的使用(中)SimpleAdapter

在前面的两篇文章中,我们讲到了关于ArrayAdapter的使用。用ArrayAdapter来在ListView中展示数据是很不错的,但是很多时候,我们的ListView中,可不只是展示文字,我们还想展示图片呢。

可能有些朋友刚才会问,第二篇不是已经可以展示图片了吗?是的呀,但是它就只能展示我们在xml中定义给它的那一张啊。

而究其原因,其实是因为我们传给它的数据源就只有字符串,没有传给图片给它,而事实上,我们也只能传文字给它,因为它用的是TextView嘛。

所以,光用ArrayAdapter显然不够丰富多彩,生活呀,总得有点不一样吧。

接下来我们就来看看在Android中关于SimpleAdapter的使用吧。

SimpleAdapter

SimpleAdapter其实一点也不simple的,因为我们需要自己去定义每个listitem的布局,如下:
?
1
2
3
4
5
6
<!--?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" >   
</textview></textview></imageview></relativelayout>
我们定义了1个ImageView控件和两个TextView控件。 接下来我们看看MainActivity中的代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
     }
}</string,></string,></map<string,></map<string,>

从上面的代码中,我们可以看到,跟ArrayAdapter类似,也要先创建一个SimpleAdapter,其构造函数如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
  * 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 > 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

http://www.2cto.com/kf/201402/278676.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值