SimpleAdapter介绍

SimpleAdapter是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。

构造函数

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

参数

  context  SimpleAdapter关联的View的运行环境

  data    一个Map组成的List。在列表中的每个条目对应列表中的一行,每一个map中应该包含所有在from参数中指定的键

  resource   一个定义列表项的布局文件的资源ID。布局文件将至少应包含那些在to中定义了的ID

  from         一个将被添加到Map映射上的键名

  to     将绑定数据的视图的ID,跟from参数对应,这些应该全是TextView

ListView

Java类

  1. package com.app.test01;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.widget.ListView;
  8. import android.widget.SimpleAdapter;
  9. public class ListViewSimple extends Activity{
  10. ListView listView1;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. // TODO Auto-generated method stub
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_weixin);
  16. listView1 = (ListView) findViewById(R.id.listView1);
  17. String[] strings = {"img","title","info","time"};//Map的key集合数组
  18. int[] ids = {R.id.img,R.id.title,R.id.info,R.id.time};//对应布局文件的id
  19. SimpleAdapter simpleAdapter = new SimpleAdapter(this,
  20. getData(), R.layout.activity_weixin_item, strings, ids);
  21. listView1.setAdapter(simpleAdapter);//绑定适配器
  22. }
  23. // 初始化一个List
  24. private List<HashMap<String, Object>> getData() {
  25. // 新建一个集合类,用于存放多条数据
  26. ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
  27. HashMap<String, Object> map = null;
  28. for (int i = 1; i <= 40; i++) {
  29. map = new HashMap<String, Object>();
  30. map.put("title", "人物" + i);
  31. map.put("time", "9月20日");
  32. map.put("info", "我通过了你的好友验证请求,现在我们可以开始对话啦");
  33. map.put("img", R.drawable.special_spring_head2);
  34. list.add(map);
  35. }
  36. return list;
  37. }
  38. }

主视图布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- 这是范例ListView的布局文件,出了ListView,还可以放置其他控件 -->
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#fff"
  7. android:orientation="vertical" >
  8. <RelativeLayout
  9. android:id="@+id/relativeLayout1"
  10. android:layout_width="match_parent"
  11. android:layout_height="50dp"
  12. android:layout_alignParentLeft="true"
  13. android:layout_alignParentTop="true"
  14. android:background="#2B3439" >
  15. <TextView
  16. android:id="@+id/textView1"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_centerHorizontal="true"
  20. android:layout_centerVertical="true"
  21. android:text="微信"
  22. android:textColor="#fff"
  23. android:textSize="22sp" />
  24. </RelativeLayout>
  25. <ListView
  26. android:id="@+id/listView1"
  27. android:layout_width="match_parent"
  28. android:paddingTop="60dp"
  29. android:paddingBottom="50dp"
  30. android:cacheColorHint="#00000000"
  31. android:layout_height="match_parent"
  32. android:stackFromBottom="true"
  33. android:transcriptMode="alwaysScroll"  >
  34. </ListView>
  35. </RelativeLayout>

子视图布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- 这是列表项的布局文件,每一行长什么样子,修改这里 -->
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="60dp"
  6. android:layout_gravity="center_vertical"
  7. android:orientation="horizontal"
  8. android:padding="5dp" >
  9. <ImageView
  10. android:id="@+id/img"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentLeft="true"
  14. android:layout_alignParentTop="true"
  15. android:layout_centerVertical="true"
  16. android:padding="5dp"
  17. android:src="@drawable/gong1" />
  18. <RelativeLayout
  19. android:id="@+id/relativeLayout1"
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent"
  22. android:layout_centerVertical="true"
  23. android:layout_marginLeft="5dp"
  24. android:layout_marginRight="70dp"
  25. android:layout_toRightOf="@+id/img" >
  26. <TextView
  27. android:id="@+id/title"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_alignParentLeft="true"
  31. android:layout_alignParentTop="true"
  32. android:text="TextView"
  33. android:textColor="#000"
  34. android:textSize="18sp"/>
  35. <TextView
  36. android:id="@+id/info"
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content"
  39. android:layout_alignParentBottom="true"
  40. android:layout_alignParentLeft="true"
  41. android:text="TextView"
  42. android:singleLine="true"
  43. android:ellipsize="end"
  44. android:textColor="#ccc"
  45. android:textSize="15sp"  />
  46. </RelativeLayout>
  47. <TextView
  48. android:id="@+id/time"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_alignTop="@+id/relativeLayout1"
  52. android:layout_alignParentRight="true"
  53. android:layout_marginRight="10dp"
  54. android:text="TextView"
  55. android:textColor="#ccc"
  56. android:textSize="15sp"/>
  57. </RelativeLayout>

效果图

 

GridView

Java类

  1. package com.app.test01;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.widget.GridView;
  8. import android.widget.SimpleAdapter;
  9. public class GridViewSimple extends Activity {
  10. private GridView gridView1;
  11. private int[] ids = { R.drawable.gong1, R.drawable.gong2, R.drawable.gong3,
  12. R.drawable.gong4, R.drawable.gong5, R.drawable.gong6,
  13. R.drawable.gong7, R.drawable.gong8, R.drawable.gong9,
  14. R.drawable.gong1, R.drawable.gong2, R.drawable.gong3,
  15. R.drawable.gong4, R.drawable.gong5, R.drawable.gong6,
  16. R.drawable.gong7, R.drawable.gong8, R.drawable.gong9 };
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. // TODO Auto-generated method stub
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.gridview);
  22. gridView1 = (GridView) findViewById(R.id.gridView1);
  23. ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
  24. for (int i = 0; i < ids.length; i++) {
  25. Map<String, Object> map = new HashMap<String, Object>();
  26. map.put("image", ids[i]);
  27. arrayList.add(map);
  28. }
  29. SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList,
  30. R.layout.gridview_item, new java.lang.String[] { "image" },
  31. new int[] { R.id.imageView1 });
  32. gridView1.setAdapter(simpleAdapter);
  33. }
  34. }

主视图布局

  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="vertical" >
  6. <GridView
  7. android:id="@+id/gridView1"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:numColumns="3" >
  11. </GridView>
  12. </LinearLayout>

子视图布局

  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="vertical"
  6. android:gravity="center"
  7. android:paddingTop="10dp"
  8. android:paddingBottom="10dp">
  9. <ImageView
  10. android:id="@+id/imageView1"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:src="@drawable/gong1" />
  14. <TextView
  15. android:id="@+id/textView1"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="TextView" />
  19. </LinearLayout>

效果图

【Android】以SimpleAdapter做适配器的ListView和GridView的更多相关文章

  1. 打造通用的Android下拉刷新组件(适用于ListView、GridView等各类View)

    前言 近期在做项目时,使用了一个开源的下拉刷新ListView组件.极其的不稳定,bug还多.稳定的组件又写得太复杂了,jar包较大.在我的一篇博客中也讲述过下拉刷新的实现,即Android打造(Li ...

  2. android 项目学习随笔十七(ListView、GridView显示组图)

    ListView.GridView显示组图,处理机制相同 <?xml version="1.0" encoding="utf-8"?> <Li ...

  3. Android——MeasureSpec学习 - 解决ScrollView嵌套ListView和GridView冲突的方法

      原文地址:http://blog.csdn.net/yuhailong626/article/details/20639217   在自定义View和ViewGroup的时候,我们经常会遇到int ...

  4. 【Android】以BaseAdapter做适配器的ListView及其性能优化

    适配器的Java类 package com.app.adapter; import org.json.JSONArray; import org.json.JSONObject; import and ...

  5. Android -- ListView(SimpleAdapter) 自定义适配器

    aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA ...

  6. Xamarin.Android 使用 SimpleAdapter 打造 ListView 万能适配器

    第一步:创建 layout1.axml 来展示列表详细内容 <?xml version="1.0" encoding="utf-8"?> <L ...

  7. 安卓开发_浅谈ListView(SimpleAdapter数组适配器)

    安卓开发_浅谈ListView(ArrayAdapter数组适配器) 学习使用ListView组件和SimapleAdapter适配器实现一个带图标的ListView列表 总共3部分 一.MainAc ...

  8. Android(java)学习笔记137:Android中SimpleAdapter,ArrayAdapter和BaseAdapter常见的适配器

    1.SimpleAdapter(BaseAdapter子类扩展类): simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片)等.可以显示比较复杂的列表, ...

  9. Android(java)学习笔记79:Android中SimpleAdapter,ArrayAdapter和BaseAdapter常见的适配器

    1. SimpleAdapter(BaseAdapter子类扩展类): simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片)等.可以显示比较复杂的列表 ...

随机推荐

  1. WPF / Win Form:多线程去修改或访问UI线程数据的方法( winform 跨线程访问UI控件 )

    WPF:谈谈各种多线程去修改或访问UI线程数据的方法http://www.cnblogs.com/mgen/archive/2012/03/10/2389509.html 子线程非法访问UI线程的数据 ...

  2. hdu 1063 Exponentiation

    求实数的幂,这个用C++写的话有点长,但是用Java写就非常方便了…… );            System.out.println(an);        }    }}

  3. 解决Unable to load R3 module ...VBoxDD.dll (VBoxDD):GetLastError=1790

    解决Unable to load R3 module ...VBoxDD.dll (VBoxDD):GetLastError=1790 参考文章:http://blog.sina.com.cn/s/b ...

  4. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

  5. 如何删除ArcSde Service服务

    1)打开“控制面板”,“服务”,找到“ArcSde Service(somename)”,这里somename就是你的ArcSde服务的真实的名字,记住这个名字(为叙述方便,以下用somename表示 ...

  6. MyEclipse 2014GA 新建 Web Project 并配置 SSH

    基本软件配置:     1)MyEclipse 2014GA(JDK:内置 1.7.0.u45:SSH:内置 Struts2.1.Spring3.1 和 Hibernate4.1) 2)apache- ...

  7. linux中proc文件系统 -- ldd3读书笔记

    1./proc 文件系统概述 /proc 文件系统是由软件创建,被内核用来向外界报告信息的一个文件系统./proc 下面的每一个文件都和一个内核函数相关联,当文件的被读取时,与之对应的内核函数用于产生 ...

  8. C# WinForm窗体 控件Control 的 Invalidate、Update、Refresh的区别

    Control.Refresh - does an Control.Invalidate followed by Control.Update.Refresh: 强制控件使其工作区无效并立即重绘自己和 ...

  9. oracle tns in linux

    [oracle@redhat4 admin]$ cd $ORACLE_HOME/network/admin[oracle@redhat4 admin]$ cat tnsnames.ora# tnsna ...

  10. hdu 4901 The Romantic Hero (dp)

    题目链接 题意:给一个数组a,从中选择一些元素,构成两个数组s, t,使s数组里的所有元素异或 等于 t数组里的所有元素 位于,求有多少种构成方式.要求s数组里 的所有的元素的下标 小于 t数组里的所 ...