【Android】以SimpleAdapter做适配器的ListView和GridView
SimpleAdapter介绍
SimpleAdapter是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。
构造函数
- 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类
- package com.app.test01;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class ListViewSimple extends Activity{
- ListView listView1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_weixin);
- listView1 = (ListView) findViewById(R.id.listView1);
- String[] strings = {"img","title","info","time"};//Map的key集合数组
- int[] ids = {R.id.img,R.id.title,R.id.info,R.id.time};//对应布局文件的id
- SimpleAdapter simpleAdapter = new SimpleAdapter(this,
- getData(), R.layout.activity_weixin_item, strings, ids);
- listView1.setAdapter(simpleAdapter);//绑定适配器
- }
- // 初始化一个List
- private List<HashMap<String, Object>> getData() {
- // 新建一个集合类,用于存放多条数据
- ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
- HashMap<String, Object> map = null;
- for (int i = 1; i <= 40; i++) {
- map = new HashMap<String, Object>();
- map.put("title", "人物" + i);
- map.put("time", "9月20日");
- map.put("info", "我通过了你的好友验证请求,现在我们可以开始对话啦");
- map.put("img", R.drawable.special_spring_head2);
- list.add(map);
- }
- return list;
- }
- }
主视图布局
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 这是范例ListView的布局文件,出了ListView,还可以放置其他控件 -->
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="fill_parent"
- android:background="#fff"
- android:orientation="vertical" >
- <RelativeLayout
- android:id="@+id/relativeLayout1"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:background="#2B3439" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="微信"
- android:textColor="#fff"
- android:textSize="22sp" />
- </RelativeLayout>
- <ListView
- android:id="@+id/listView1"
- android:layout_width="match_parent"
- android:paddingTop="60dp"
- android:paddingBottom="50dp"
- android:cacheColorHint="#00000000"
- android:layout_height="match_parent"
- android:stackFromBottom="true"
- android:transcriptMode="alwaysScroll" >
- </ListView>
- </RelativeLayout>
子视图布局
- <?xml version="1.0" encoding="utf-8"?>
- <!-- 这是列表项的布局文件,每一行长什么样子,修改这里 -->
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="60dp"
- android:layout_gravity="center_vertical"
- android:orientation="horizontal"
- android:padding="5dp" >
- <ImageView
- android:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:layout_centerVertical="true"
- android:padding="5dp"
- android:src="@drawable/gong1" />
- <RelativeLayout
- android:id="@+id/relativeLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_centerVertical="true"
- android:layout_marginLeft="5dp"
- android:layout_marginRight="70dp"
- android:layout_toRightOf="@+id/img" >
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:text="TextView"
- android:textColor="#000"
- android:textSize="18sp"/>
- <TextView
- android:id="@+id/info"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_alignParentLeft="true"
- android:text="TextView"
- android:singleLine="true"
- android:ellipsize="end"
- android:textColor="#ccc"
- android:textSize="15sp" />
- </RelativeLayout>
- <TextView
- android:id="@+id/time"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignTop="@+id/relativeLayout1"
- android:layout_alignParentRight="true"
- android:layout_marginRight="10dp"
- android:text="TextView"
- android:textColor="#ccc"
- android:textSize="15sp"/>
- </RelativeLayout>
效果图

GridView
Java类
- package com.app.test01;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.GridView;
- import android.widget.SimpleAdapter;
- public class GridViewSimple extends Activity {
- private GridView gridView1;
- private int[] ids = { R.drawable.gong1, R.drawable.gong2, R.drawable.gong3,
- R.drawable.gong4, R.drawable.gong5, R.drawable.gong6,
- R.drawable.gong7, R.drawable.gong8, R.drawable.gong9,
- R.drawable.gong1, R.drawable.gong2, R.drawable.gong3,
- R.drawable.gong4, R.drawable.gong5, R.drawable.gong6,
- R.drawable.gong7, R.drawable.gong8, R.drawable.gong9 };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.gridview);
- gridView1 = (GridView) findViewById(R.id.gridView1);
- ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
- for (int i = 0; i < ids.length; i++) {
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("image", ids[i]);
- arrayList.add(map);
- }
- SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList,
- R.layout.gridview_item, new java.lang.String[] { "image" },
- new int[] { R.id.imageView1 });
- gridView1.setAdapter(simpleAdapter);
- }
- }
主视图布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <GridView
- android:id="@+id/gridView1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:numColumns="3" >
- </GridView>
- </LinearLayout>
子视图布局
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center"
- android:paddingTop="10dp"
- android:paddingBottom="10dp">
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/gong1" />
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView" />
- </LinearLayout>
效果图

【Android】以SimpleAdapter做适配器的ListView和GridView的更多相关文章
- 打造通用的Android下拉刷新组件(适用于ListView、GridView等各类View)
前言 近期在做项目时,使用了一个开源的下拉刷新ListView组件.极其的不稳定,bug还多.稳定的组件又写得太复杂了,jar包较大.在我的一篇博客中也讲述过下拉刷新的实现,即Android打造(Li ...
- android 项目学习随笔十七(ListView、GridView显示组图)
ListView.GridView显示组图,处理机制相同 <?xml version="1.0" encoding="utf-8"?> <Li ...
- Android——MeasureSpec学习 - 解决ScrollView嵌套ListView和GridView冲突的方法
原文地址:http://blog.csdn.net/yuhailong626/article/details/20639217 在自定义View和ViewGroup的时候,我们经常会遇到int ...
- 【Android】以BaseAdapter做适配器的ListView及其性能优化
适配器的Java类 package com.app.adapter; import org.json.JSONArray; import org.json.JSONObject; import and ...
- Android -- ListView(SimpleAdapter) 自定义适配器
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA ...
- Xamarin.Android 使用 SimpleAdapter 打造 ListView 万能适配器
第一步:创建 layout1.axml 来展示列表详细内容 <?xml version="1.0" encoding="utf-8"?> <L ...
- 安卓开发_浅谈ListView(SimpleAdapter数组适配器)
安卓开发_浅谈ListView(ArrayAdapter数组适配器) 学习使用ListView组件和SimapleAdapter适配器实现一个带图标的ListView列表 总共3部分 一.MainAc ...
- Android(java)学习笔记137:Android中SimpleAdapter,ArrayAdapter和BaseAdapter常见的适配器
1.SimpleAdapter(BaseAdapter子类扩展类): simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片)等.可以显示比较复杂的列表, ...
- Android(java)学习笔记79:Android中SimpleAdapter,ArrayAdapter和BaseAdapter常见的适配器
1. SimpleAdapter(BaseAdapter子类扩展类): simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片)等.可以显示比较复杂的列表 ...
随机推荐
- UVA 10246 Asterix and Obelix
题意:每个城市举办庆祝有一定的花费,A在路径上会选择庆祝花费最大的城市 让你求,A回家所花的路费和庆祝费最少,也就是说并不是最短路径就是结果, 还有可能就是路费比最短路径的多,但是庆祝费就比它的少,总 ...
- POJ 2240 Arbitrage(floyd)
http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...
- 李洪强iOS开发之最全App上架流程
在上架App之前想要 真机测试的同学 请查看 iOS- 最全的真机测试教程 里面包含怎么让多台电脑同时 上架App和同时真机调试.P12文件的使用详解 准备 开发者账号 完工的项目 上架步骤 一.创建 ...
- 李洪强iOS开发支付集成之银联支付
iOS开发支付集成之银联支付 银联官网在这里,这里能下载SDK或者是看文档.最新的版本写的简单了很多,看文档一直做下去基本上就没问题了. 首先,SDK在这里下载,里面包含需要的库文件和详细的文档. 银 ...
- SpringMVC学习总结(六)——SpringMVC文件上传例子(2)
基本的SpringMVC的搭建在我的上一篇文章里已经写过了,这篇文章主要说明一下使用SpringMVC进行表单上的文件上传以及多个文件同时上传的不同方法 一.配置文件: SpringMVC 用的是 的 ...
- mysql字段的适当冗余有利于提高查询速度
CREATE TABLE `comment` ( `c_id` int(11) NOT NULL auto_increment COMMENT '评论ID', `u_id` int(11) NOT ...
- Pascal编译器大全(非常难得)
http://www.pascaland.org/pascall.htm Some titles (french) : Compilateurs Pascal avec sources = compi ...
- ssm框架整合小结
1.整合思路 一.Dao层:整合mybatis和spring 需要的jar包: 1.mybatis的jar包 2.Mysql数据库驱动 3.数据库连接池 4.Mybatis和spring的整合包. 5 ...
- CentOS 7.0安装Nvidia驱动
entOS 7.0 Nvidia显卡安装步骤: 1 在英伟达官网下载相应驱动 搜索出相应的驱动后,不要直接点,而是右健,Save Link as... 否则,会出现下载半天没动静的情况. 存放的路径上 ...
- 纯后台生成highcharts图片有哪些方法?
比如说,领导抛给你一个需求,把一些数据做成图表,每天通过邮件发送,让领导能在邮件中就看到图片,你会有什么思路呢?本人使用的是phantomjs这个神器,它的内核是WebKit引擎,不提供图形界面,只能 ...