SimpleAdapter

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 plain  copy
  在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 plain  copy
  在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 plain  copy
  在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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值