Android之SimpleAdapter简单实例和SimpleAdapter参数说明(zt)

http://blog.csdn.net/x605940745/article/details/11981049

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
  1. <LinearLayoutxmlns: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. <ListView
  8. android:id="@+id/lt1"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content">
  11. </ListView>
  12. </LinearLayout>

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <ImageView
  7. android:id="@+id/head"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:paddingLeft="10dp"/>
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:orientation="vertical">
  15. <TextView
  16. android:id="@+id/name"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:textSize="20dp"
  20. android:textColor="#f0f"
  21. android:paddingLeft="10dp"/>
  22. <TextView
  23. android:id="@+id/desc"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:textSize="14dp"
  27. android:paddingLeft="10dp"/>
  28. </LinearLayout>
  29. </LinearLayout>


[java] view plain copy
  1. packagecom.example.simpleadptertest;
  2. importjava.util.ArrayList;
  3. importjava.util.HashMap;
  4. importjava.util.List;
  5. importjava.util.Map;
  6. importandroid.app.Activity;
  7. importandroid.os.Bundle;
  8. importandroid.view.Menu;
  9. importandroid.widget.ListView;
  10. importandroid.widget.SimpleAdapter;
  11. publicclassMainActivityextendsActivity{
  12. privateString[]name={"剑萧舞蝶","张三","hello","诗情画意"};
  13. privateString[]desc={"魔域玩家","百家执行","高级的富一代","妹子请过来..一个善于跑妹子的。。"};
  14. privateint[]imageids={R.drawable.libai,R.drawable.nongyu,
  15. R.drawable.qingzhao,R.drawable.tiger};
  16. privateListViewlt1;
  17. @Override
  18. protectedvoidonCreate(BundlesavedInstanceState){
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. List<Map<String,Object>>listems=newArrayList<Map<String,Object>>();
  22. for(inti=0;i<name.length;i++){
  23. Map<String,Object>listem=newHashMap<String,Object>();
  24. listem.put("head",imageids[i]);
  25. listem.put("name",name[i]);
  26. listem.put("desc",desc[i]);
  27. listems.add(listem);
  28. }
  29. /*SimpleAdapter的参数说明
  30. *第一个参数表示访问整个android应用程序接口,基本上所有的组件都需要
  31. *第二个参数表示生成一个Map(String,Object)列表选项
  32. *第三个参数表示界面布局的id表示该文件作为列表项的组件
  33. *第四个参数表示该Map对象的哪些key对应value来生成列表项
  34. *第五个参数表示来填充的组件Map对象key对应的资源一依次填充组件顺序有对应关系
  35. *注意的是map对象可以key可以找不到但组件的必须要有资源填充因为找不到key也会返回null其实就相当于给了一个null资源
  36. *下面的程序中如果newString[]{"name","head","desc","name"}newint[]{R.id.name,R.id.head,R.id.desc,R.id.head}
  37. *这个head的组件会被name资源覆盖
  38. **/
  39. SimpleAdaptersimplead=newSimpleAdapter(this,listems,
  40. R.layout.simple_item,newString[]{"name","head","desc"},
  41. newint[]{R.id.name,R.id.head,R.id.desc});
  42. lt1=(ListView)findViewById(R.id.lt1);
  43. lt1.setAdapter(simplead);
  44. }
  45. @Override
  46. publicbooleanonCreateOptionsMenu(Menumenu){
  47. //Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
  48. getMenuInflater().inflate(R.menu.main,menu);
  49. returntrue;
  50. }
  51. }


更多相关文章

  1. Android四大组件之~~Service
  2. Android七大布局
  3. Android(安卓)NDK开发之旅38--FFmpeg视频添加水印
  4. Android(安卓)CountDownTimer实现定时器和倒计时效果
  5. Android(安卓)中文API (61) ―― ViewSwitcher
  6. TextView 限制最大行数、最小行数、字数超过“...”表示
  7. 关于Android中的radioGroup选择
  8. mybatisplus的坑 insert标签insert into select无参数问题的解决
  9. Python技巧匿名函数、回调函数和高阶函数

随机推荐

  1. 使用jenkins自动上传IOS,android到阿里云
  2. Android代码优化——使用Android(安卓)li
  3. 在android的状态栏(statusbar)中增加menu
  4. 十八般武艺!移动应用开发者必备的18款利器
  5. Android(安卓)SoftAp支持 (一)
  6. android 读取assets文件夹下的文件资源
  7. Android开发中后台的Service服务探索
  8. 使用NDK移植开源项目,JNI的使用技巧
  9. Android(安卓)群英传-第五章:Android(安
  10. android drawBitmapMesh and drawVertice