在《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的更多相关文章

  1. Android 常用数据适配器ArrayAdapter

    接着上篇文章<Android 采用Layout Inflater创建一个View对象>,本文采用常用数据适配器ArrayAdapter 新建项目后,在layout文件夹下新建list_it ...

  2. 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)

    1.listview入门,自定义的数据适配器 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  3. Android 常用数据操作封装类案例

    1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils; import android.content.Conte ...

  4. Android常用数据类型转换

    String转int.float.double.byte[].bitmap Int i = Integer.parseInt(str); Float f = Float.parseFloat(str) ...

  5. Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式

    Android开发工程师文集-Fragment,适配器,轮播图,ScrollView,Gallery 图片浏览器,Android常用布局样式 Fragment FragmentManager frag ...

  6. Android学习之适配器ArrayAdapter SimpleAdapter

    Adapter是个什么角色呢?其实它的作用就是View界面和数据之间的桥梁.我们可以看作是界面数据绑定的一种理解,它所操纵的数据一般都是一些比较复杂的数据,如数组,链表,数据库,集合等. 常用的适配器 ...

  7. android 适配器simpleadapter和baseadapter区别

    android 适配器 simpleadapter 和 baseadapter 设计网络程序或者数据处理显示程序的时候,常常会使用 simpleadapter 和baseadapter 来实现. ad ...

  8. Android 数据适配器

    把复杂的数据(数组.链表.数据库.集合等)填充到指定的视图界面上.   arrayAdapter(数组适配器):      用于绑定一些格式单一的数据,数据源:数据或者集合.   private Li ...

  9. [置顶] Android常用适配器控件

    Android常用适配器控件 列表控件用于显示数据集合,Android不是使用一种类型的控件管理显示和数据,而是将这两项功能分布用列表控件和适配器来实现.列表控件扩展了android.widget.A ...

随机推荐

  1. Halcon与opencv格式的转换

    IplImage* ImageProcess::HImageToIplImage(Hobject &Hobj) { IplImage* pImage; HTuple htChannels; c ...

  2. CSS3点点滴滴

    css查遗补漏 标签(空格分隔): css 如果值为若干单词,则要给值加引号.p {font-family: "sans serif";} body的字体有继承兼容性问题,通常这么 ...

  3. Git学习笔记(二)

    一.创建远程仓库(GitHub) 1.GitHub网站地址:https://github.com/,这个网站就是提供Git仓库托管服务的,所以,只要注册一个GitHub账号,就可以免费获得Git远程仓 ...

  4. 解析导航栏的url--selnium,beautifulsoup实战

    前段时间做ui自动化测试的时候,导航栏菜单始终有点问题,最后只好直接获取到url,然后直接使用driver.get(url)进入页面: 包括做压测的时候,比如我要找出所有报表菜单的url,这样不可能手 ...

  5. LeetCode - Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  6. C语言学习002:第一个完整的C程序代码

    #include <stdio.h>//引用相关的外部库,stdio.h包含了终端读写数据的代码 //程序入口,程序通过main函数的返回值判断程序是否运行成功,0表示成功,非0表示程序运 ...

  7. 如何在Winform界面中设计图文并茂的界面

    在Winform里面,很多控件元素都是标准的,如图标.按钮.工具栏等等,所以一般设计标准的Winform界面比较快捷,但是往往这样的界面相对单调一些,特别在界面控件比较少的情况下,我们往往需要加入一些 ...

  8. 在Winform开发中使用日程控件XtraScheduler(2)--深入理解数据的存储

    在上篇随笔<在Winform开发中使用日程控件XtraScheduler>中介绍了DevExpress的XtraScheduler日程控件的各种使用知识点,对于我们来说,日程控件不陌生,如 ...

  9. 关于ExtJs4的Grid带 查询 参数 分页(baseParams-->extraParams)

    (园里很多文章,美名其曰 :ExtJs GridPanel+查询条件+分页.  但是加查询条件后点击下一页,查询条件失效,求你们自己测试明白再显摆 不要误导我这种新人.) ExtJs6发布了,ExtJ ...

  10. 关于SilverLight

    目前工作中用到SilverLight,有必要对其作一定的了解.