Android 常用数据适配器SimpleAdapter
在《Android 常用数据适配器ArrayAdapter》中介绍了ArrayAdapter数据适配器。但是存在一个缺陷,那就是条目的图标都固定相同,要显示每个条目的图标都不相同,那么使用SimpleAdapter
新建项目后,在layout文件夹下新建list_item.xml文件,接着编辑布局,代码如下:
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" > <ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>
activity_main.xml中的代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> </RelativeLayout>
从……\sdk\platforms\android-18\data\res\drawable-hdpi中随便拷贝几个图片,放到drawable-hdpi文件夹中
代码如下:
package com.wuyudong.simpleadapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = (ListView) findViewById(R.id.lv);
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("nametext", "我是第1个功能");
map1.put("iconid", R.drawable.btn_radio_off_disabled_focused_holo_dark); Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("nametext", "我是第2个功能");
map2.put("iconid", R.drawable.btn_radio_off_disabled_focused_holo_light); Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("nametext", "我是第3个功能");
map3.put("iconid", R.drawable.btn_radio_off_focused_holo_dark); Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("nametext", "我是第4个功能");
map4.put("iconid", R.drawable.btn_radio_off_focused_holo_light); Map<String, Object> map5 = new HashMap<String, Object>();
map5.put("nametext", "我是第5个功能");
map5.put("iconid", R.drawable.btn_radio_off_holo); data.add(map1);
data.add(map2);
data.add(map3);
data.add(map4);
data.add(map5); //data绑定的数据. list集合
//R.layout.list_item. 数据显示对应的布局
//要让数据里的view对象建立一个映射关系
//from[] map集合里面数据的key
//to[] 布局文件里面的 id
lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item,
new String[] { "nametext", "iconid" }, new int[]{R.id.tv, R.id.iv})); }
}
运行后的效果如下:

Android 常用数据适配器SimpleAdapter的更多相关文章
- Android 常用数据适配器ArrayAdapter
接着上篇文章<Android 采用Layout Inflater创建一个View对象>,本文采用常用数据适配器ArrayAdapter 新建项目后,在layout文件夹下新建list_it ...
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- Android 常用数据操作封装类案例
1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils; import android.content.Conte ...
- Android常用数据类型转换
String转int.float.double.byte[].bitmap Int i = Integer.parseInt(str); Float f = Float.parseFloat(str) ...
- Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式
Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式 Fragment FragmentManager frag ...
- Android学习之适配器ArrayAdapter SimpleAdapter
Adapter是个什么角色呢?其实它的作用就是View界面和数据之间的桥梁.我们可以看作是界面数据绑定的一种理解,它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等. 常用的适配器 ...
- android 适配器simpleadapter和baseadapter区别
android 适配器 simpleadapter 和 baseadapter 设计网络程序或者数据处理显示程序的时候,常常会使用 simpleadapter 和baseadapter 来实现. ad ...
- Android 数据适配器
把复杂的数据(数组.链表.数据库.集合等)填充到指定的视图界面上. arrayAdapter(数组适配器): 用于绑定一些格式单一的数据,数据源:数据或者集合. private Li ...
- [置顶] Android常用适配器控件
Android常用适配器控件 列表控件用于显示数据集合,Android不是使用一种类型的控件管理显示和数据,而是将这两项功能分布用列表控件和适配器来实现.列表控件扩展了android.widget.A ...
随机推荐
- SQL Server 2014如何提升非在线的在线操作
在今天的文章里,我想谈下在线索引重建操作( Online Index Rebuild operations),它们在SQL Server 2014里有怎样的提升.我们都知道,自SQL Server 2 ...
- laravel中的错误与日志
日志 laravel中的日志是基于monolog而封装的.laravel在它上面做了几个事情: 把monolog中的addInfo等函数简化成为了info这样的函数 增加了useFiles和useDa ...
- 【视频处理】YUV格式说明
YUV,是一种颜色编码方法,Y表示明亮度(Luminance.Luma),U和V则是色度.浓度(Chrominance.Chroma). YUV,Y`UV,YCbCr,YPbPr等都可以称为YUV,彼 ...
- 使用NuGet打包并发布至ProGet过程 (步骤详细,附python脚本)【上篇】
一.基本知识 (1)NuGet : NuGet是一个为大家所熟知的Visual Studio扩展,通过这个扩展,开发人员可以非常方便地在Visual Studio中安装或更新项目中所需要的第三方组件, ...
- C#将集合快速排序
C#实现集合排序类. 说明: 1.集合类型参数化: 2.可根据集合中的对象的各个属性进行排序,传入属性名称即可: 注:属性必须实现了IComparable接口,C#中int.datetime.stri ...
- Js模型和封装
一点拙劣的小分享,欢迎批评和补充 我们经常在Js中为我们的各类组件,构造Json格式的假数据.我们看如下代码: var jsonResult = [ { ' }, { ' } //我们把jsonRes ...
- winform自定义日期控件,要求可以手动输入日期DatePicker
要求:文本框中能手动输入数字,向上箭头根据鼠标位置给年月日递增,向下箭头递减 一:页面加载时: private void FlatDatePicker_Load(object sender, Even ...
- HTML标签小结
HTML:超文本标记语言 超:超链接 超文本:超出文本(可加入图片,文字,音频视频播放器) 标记:标签 HTML文档 以<html...>开始 , 以</html> ...
- NOSQL学习笔记系列之MongoDB 一 基础
主题:MongoDB 学习资料参考网址: 1.http://www.w3cschool.cc/mongodb/mongodb-tutorial.html 2.http://www.icoolxue.c ...
- Atitit.js javascript的rpc框架选型
Atitit.js javascript的rpc框架选型 1. Dwr1 2. 使用AJAXRPC1 2.2. 数据类型映射表1 3. json-rpc轻量级远程调用协议介绍及使用2 3.1. 2.3 ...