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

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

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

 咱们能够使用SimpleAdapter+ListView来实现。函数

 SimpleAdapter其中一个构造函数以下:布局

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

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

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

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

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

2:能够为项模板指定一个匹配的数据xml

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

复制代码  

<? 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"         >      <      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);  
           

 

复制代码  

看看运行截图吧:

相关文章
相关标签/搜索