Android控件——ListView之Adapter提供数据(其二)

上一节中一些列表集合数据到手机屏幕时,通常采用ListView组件+ArrayAdapter.

虽然它能为我们提供展示数据列表的能力,但是展示的项却不能定制,如果我们的项是由2个TextView组成的,它就无能为力了。项目中大部分的不单单是展示简单的项模板,更多时候,我们可以对项模板进行一些定制,来满足我们的需求,假设项模板需要展示2个TextView 呢?怎么办?

 我们可以使用SimpleAdapter+ListView来实现。

 SimpleAdapter其中一个构造函数如下:

public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to) 

 第一个参数:当前上下文对象。第二个参数:一个List类型的泛型集合,泛型类型必须继承之Map类型。第三个:布局资源的ID,

 第四个参数:需要绑定的Key列表。第五个参数:与Key列表项对应的资源文件中的具体组件ID集合。

有以上的理论基础,我们知道使用SimpleAdapter会带来这样的好处:

1:可以自定义任何项模板,自由度很高(取决于美工水平)

2:可以为项模板指定一个匹配的数据

我们先来看看项模板的布局,很简单,就是一个ImageView,两个TextView

复制代码  

<?
 xml version="1.0" encoding="utf-8"   ?>    <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent" android:layout_height
    
    
    
 
     ="match_parent"
         >
      <
      ImageView        android:layout_width  ="wrap_content"
    
    
    
 
              android:layout_height
    
    
    
 
     ="wrap_content"
    
    
    
 
             android:id
    
    
    
 
     ="@+id/img"
    
    
    
 
     >
    
    
    
 
           
    
    
    
 
     </
    
    
    
 
     ImageView
    
    
    
 
     >
    
    
    
 
             
    
    
    
 
     <
    
    
    
 
     TextView   
    
    
    
 
     android:id
    
    
    
 
     ="@+id/txtName"
    
    
    
 
        android:layout_width
    
    
    
 
     ="wrap_content"
    
    
    
 
        android:layout_height
    
    
    
 
     ="wrap_content"
    
    
    
 
       
    
    
    
 
     >
    
    
    
 
       
    
    
    
 
     </
    
    
    
 
     TextView
    
    
    
 
     >
    
    
    
 
         
    
    
    
 
     <
    
    
    
 
     TextView   
    
    
    
 
     android:paddingLeft
    
    
    
 
     ="20sp"
    
    
    
 
        android:id
    
    
    
 
     ="@+id/txtLength"
    
    
    
 
        android:layout_width
    
    
    
 
     ="wrap_content"
    
    
    
 
        android:layout_height
    
    
    
 
     ="wrap_content"
    
    
    
 
       
    
    
    
 
     >
    
    
    
 
       
    
    
    
 
     </
    
    
    
 
     TextView
    
    
    
 
     >
    
    
    
 
       
    
    
    
 
     </
    
    
    
 
     LinearLayout
    
    
    
 
     >
   
   
   
   

   

  

复制代码  

我们可以定义数据源,当然这个数据源是继承自List集合接口的,并且类型为基础Map接口。如下:

复制代码  

List
    
    
    
 
     <
    
    
    
 
     Map
    
    
    
 
     <
    
    
    
 
     String,Object
    
    
    
 
     >>
    
    
    
 
      lists
    
    
    
 
     =
    
    
    
 
     new
    
    
    
 
      ArrayList
    
    
    
 
     <
    
    
    
 
     Map
    
    
    
 
     <
    
    
    
 
     String,Object
    
    
    
 
     >>
    
    
    
 
     ();             
    
    
    
 
     for
    
    
    
 
     (
    
    
    
 
     int
    
    
    
 
      i
    
    
    
 
     =
    
    
    
 
     0
    
    
    
 
     ;i
    
    
    
 
     <
    
    
    
 
     4
    
    
    
 
     ;i
    
    
    
 
     ++
    
    
    
 
     ){                 Map
    
    
    
 
     <
    
    
    
 
     String,Object
    
    
    
 
     >
    
    
    
 
      map
    
    
    
 
     =
    
    
    
 
     new
    
    
    
 
      HashMap
    
    
    
 
     <
    
    
    
 
     String,Object
    
    
    
 
     >
    
    
    
 
     ();                 map.put(
    
    
    
 
     "
    
    
    
 
     img
    
    
    
 
     "
    
    
    
 
     , R.drawable.icon);                 map.put(
    
    
    
 
     "
    
    
    
 
     name
    
    
    
 
     "
    
    
    
 
     , 
    
    
    
 
     "
    
    
    
 
     SimpleAdapter
    
    
    
 
     "
    
    
    
 
     +
    
    
    
 
     i);                 map.put(
    
    
    
 
     "
    
    
    
 
     length
    
    
    
 
     "
    
    
    
 
     , 
    
    
    
 
     "
    
    
    
 
     300
    
    
    
 
     "
    
    
    
 
     );                 lists.add(map);             }
   
   
   
   

   

  

复制代码  

然后我们希望绑定这些数据,到指定的组件中去,全部代码如下:

复制代码  

List
    
    
    
 
     <
    
    
    
 
     Map
    
    
    
 
     <
    
    
    
 
     String,Object
    
    
    
 
     >>
    
    
    
 
      lists
    
    
    
 
     =
    
    
    
 
     new
    
    
    
 
      ArrayList
    
    
    
 
     <
    
    
    
 
     Map
    
    
    
 
     <
    
    
    
 
     String,Object
    
    
    
 
     >>
    
    
    
 
     ();             
    
    
    
 
     for
    
    
    
 
     (
    
    
    
 
     int
    
    
    
 
      i
    
    
    
 
     =
    
    
    
 
     0
    
    
    
 
     ;i
    
    
    
 
     <
    
    
    
 
     4
    
    
    
 
     ;i
    
    
    
 
     ++
    
    
    
 
     ){                 Map
    
    
    
 
     <
    
    
    
 
     String,Object
    
    
    
 
     >
    
    
    
 
      map
    
    
    
 
     =
    
    
    
 
     new
    
    
    
 
      HashMap
    
    
    
 
     <
    
    
    
 
     String,Object
    
    
    
 
     >
    
    
    
 
     ();                 map.put(
    
    
    
 
     "
    
    
    
 
     img
    
    
    
 
     "
    
    
    
 
     , R.drawable.icon);                 map.put(
    
    
    
 
     "
    
    
    
 
     name
    
    
    
 
     "
    
    
    
 
     , 
    
    
    
 
     "
    
    
    
 
     SimpleAdapter
    
    
    
 
     "
    
    
    
 
     +
    
    
    
 
     i);                 map.put(
    
    
    
 
     "
    
    
    
 
     length
    
    
    
 
     "
    
    
    
 
     , 
    
    
    
 
     "
    
    
    
 
     300
    
    
    
 
     "
    
    
    
 
     );                 lists.add(map);             }                          String []from
    
    
    
 
     =
    
    
    
 
     {
    
    
    
 
     "
    
    
    
 
     img
    
    
    
 
     "
    
    
    
 
     ,
    
    
    
 
     "
    
    
    
 
     name
    
    
    
 
     "
    
    
    
 
     ,
    
    
    
 
     "
    
    
    
 
     length
    
    
    
 
     "
    
    
    
 
     };             
    
    
    
 
     int
    
    
    
 
     []to
    
    
    
 
     =
    
    
    
 
     {R.id.img,R.id.txtName,R.id.txtLength};             SimpleAdapter adapter
    
    
    
 
     =
    
    
    
 
     new
    
    
    
 
      SimpleAdapter(
    
    
    
 
     this
    
    
    
 
     ,lists, R.layout.image, from, to);             listView.setAdapter(adapter);  
   
   
   
   

   

  

复制代码  

看看运行截图吧:

评论

暂无评论