zoukankan      html  css  js  c++  java
  • Android之SimpleAdapter简单实例和SimpleAdapter参数说明

    SimpleAdapter基本上认知了其参数含义 用起来就简单多了
    SimpleAdapter的参数说明
     第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要
     第二个参数表示生成一个Map(String ,Object)列表选项
     第三个参数表示界面布局的id  表示该文件作为列表项的组件
     第四个参数表示该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资源覆盖


    代码

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    2.     xmlns:tools="http://schemas.android.com/tools"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:orientation="horizontal"  
    6.     tools:context=".MainActivity" >  
    7.   
    8.     <ListView  
    9.         android:id="@+id/lt1"  
    10.         android:layout_width="match_parent"  
    11.         android:layout_height="wrap_content" >  
    12.     </ListView>  
    13.   
    14. </LinearLayout>  

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="match_parent"  
    4.     android:layout_height="match_parent"  
    5.     android:orientation="horizontal" >  
    6.   
    7.     <ImageView  
    8.         android:id="@+id/head"  
    9.         android:layout_width="wrap_content"  
    10.         android:layout_height="wrap_content"  
    11.         android:paddingLeft="10dp" />  
    12.   
    13.     <LinearLayout  
    14.         android:layout_width="match_parent"  
    15.         android:layout_height="wrap_content"  
    16.         android:orientation="vertical" >  
    17.           
    18.         <TextView   
    19.             android:id="@+id/name"  
    20.             android:layout_width="wrap_content"  
    21.             android:layout_height="wrap_content"  
    22.             android:textSize="20dp"  
    23.             android:textColor="#f0f"  
    24.             android:paddingLeft="10dp"/>  
    25.           
    26.                   
    27.         <TextView   
    28.             android:id="@+id/desc"  
    29.             android:layout_width="wrap_content"  
    30.             android:layout_height="wrap_content"  
    31.             android:textSize="14dp"  
    32.             android:paddingLeft="10dp"/>  
    33.           
    34.     </LinearLayout>  
    35.   
    36. </LinearLayout>  


    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    1. package com.example.simpleadptertest;  
    2.   
    3. import java.util.ArrayList;  
    4. import java.util.HashMap;  
    5. import java.util.List;  
    6. import java.util.Map;  
    7.   
    8. import android.app.Activity;  
    9. import android.os.Bundle;  
    10. import android.view.Menu;  
    11. import android.widget.ListView;  
    12. import android.widget.SimpleAdapter;  
    13.   
    14. public class MainActivity extends Activity {  
    15.   
    16.     private String[] name = { "剑萧舞蝶""张三""hello""诗情画意" };  
    17.   
    18.     private String[] desc = { "魔域玩家""百家执行""高级的富一代""妹子请过来..一个善于跑妹子的。。" };  
    19.   
    20.     private int[] imageids = { R.drawable.libai, R.drawable.nongyu,  
    21.             R.drawable.qingzhao, R.drawable.tiger };  
    22.       
    23.     private ListView lt1;  
    24.   
    25.     @Override  
    26.     protected void onCreate(Bundle savedInstanceState) {  
    27.         super.onCreate(savedInstanceState);  
    28.         setContentView(R.layout.activity_main);  
    29.         List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();  
    30.         for (int i = 0; i < name.length; i++) {  
    31.             Map<String, Object> listem = new HashMap<String, Object>();  
    32.             listem.put("head", imageids[i]);  
    33.             listem.put("name", name[i]);  
    34.             listem.put("desc", desc[i]);  
    35.             listems.add(listem);  
    36.         }  
    37.           
    38.         /*SimpleAdapter的参数说明 
    39.          * 第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要 
    40.          * 第二个参数表示生成一个Map(String ,Object)列表选项 
    41.          * 第三个参数表示界面布局的id  表示该文件作为列表项的组件 
    42.          * 第四个参数表示该Map对象的哪些key对应value来生成列表项 
    43.          * 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系 
    44.          * 注意的是map对象可以key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源 
    45.          * 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head} 
    46.          * 这个head的组件会被name资源覆盖 
    47.          * */  
    48.         SimpleAdapter simplead = new SimpleAdapter(this, listems,  
    49.                 R.layout.simple_item, new String[] { "name""head""desc" },  
    50.                 new int[] {R.id.name,R.id.head,R.id.desc});  
    51.           
    52.         lt1=(ListView)findViewById(R.id.lt1);  
    53.         lt1.setAdapter(simplead);  
    54.           
    55.     }  
    56.   
    57.     @Override  
    58.     public boolean onCreateOptionsMenu(Menu menu) {  
    59.         // Inflate the menu; this adds items to the action bar if it is present.  
    60.         getMenuInflater().inflate(R.menu.main, menu);  
    61.         return true;  
    62.     }  
    63.   
    64. }  


    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    PHP发送邮件
    SQL删除字段及判断字段是否存在的方法
    密码MySQL的root的密码
    java socket 最简单的例子(server 多线程)
    php编写最简单的webservice
    SQL Server 存储过程与触发器
    手动创建最简单的JSP 文件
    Oracle 卸载步骤
    编写 WebService 程序
    eclipse 常用快捷键
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834737.html
Copyright © 2011-2022 走看看