举例讲解Android应用中SimpleAdapter简单适配器的使用

2019-12-10 18:12:01丽君
  •  第四个参数表示该Map对象的哪些key对应value来生成列表项
  •  第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
  •  注意的是map对象可以key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源

     下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}

    这个head的组件会被name资源覆盖

    示例代码

    <LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
      xmlns:tools="http://www.easck.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      tools:context=".MainActivity" > 
     
      <ListView 
        android:id="@+id/lt1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" > 
      </ListView> 
     
    </LinearLayout> 
    
    
    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" > 
     
      <ImageView 
        android:id="@+id/head" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:paddingLeft="10dp" /> 
     
      <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" > 
         
        <TextView  
          android:id="@+id/name" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textSize="20dp" 
          android:textColor="#f0f" 
          android:paddingLeft="10dp"/> 
         
             
        <TextView  
          android:id="@+id/desc" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:textSize="14dp" 
          android:paddingLeft="10dp"/> 
         
      </LinearLayout> 
     
    </LinearLayout>