Android 常用数据适配器SimpleAdapter

时间:2021-08-11 04:25:53

在《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 常用数据适配器SimpleAdapter的更多相关文章

  1. Android 常用数据适配器ArrayAdapter

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

  2. 无废话Android之listview入门&comma;自定义的数据适配器、采用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. &lbrack;置顶&rsqb; Android常用适配器控件

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

随机推荐

  1. Java学习之LinkedHashMap学习总结

    前言: 在学习LRU算法的时候,看到LruCache源码实现是基于LinkedHashMap,今天学习一下LinkedHashMap的好处以及如何实现lru缓存机制的. 需求背景: LRU这个算法就是 ...

  2. 熟悉MyEclipse

    资源网址:http://www.myeclipsecn.com/learningcenter/ 20151126 [从这里开始]量身打造自己的MyEclipse(多图) 主要讲在MyEclipse里面 ...

  3. 多线程在iOS开发中的应用

    多线程基本概念 01 进程 进程是指在系统中正在运行的一个应用程序.每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内. 02 线程 2-1 基本概念 1个进程要想执行任务,必须得有线程 ...

  4. 找出图像I的代数中心

    function centerGPos = cenP(I ) %cenP finds the core of the PSF % [row, col] = find(I > ); minRow ...

  5. 226&period; Invert Binary Tree(C&plus;&plus;)

    226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...

  6. SOLOWHEEL - 电动独轮车 - SOLOWHEEL俱乐部聚会活动火热报名中

    SOLOWHEEL - 电动独轮车 - SOLOWHEEL俱乐部聚会活动火热报名中 SOLOWHEEL俱乐部聚会活动火热报名中

  7. MongoDB--Getting Started with Java Driver

    原文链接 http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/  介绍 本文的目的是让你对怎样使用M ...

  8. 46&period;Odoo产品分析 &lpar;五&rpar; – 定制板块&lpar;2&rpar; – 为业务自定义odoo&lpar;1&rpar;

    查看Odoo产品分析系列--目录 在这一章节中,将学习到如何设置"开发者模式"以及备份数据库:然后学习如何添加字段到数据库并在表单和视图中显示. 1 了解odoo的构架 每一个应用 ...

  9. 主流数据库连接池性能比较 hikari druid c3p0 dbcp jdbc

    背景 对现有的数据库连接池做调研对比,综合性能,可靠性,稳定性,扩展性等因素选出推荐出最优的数据库连接池 . NOTE: 本文所有测试均是MySQL库 测试结论 1:性能方面 hikariCP> ...

  10. MySQL安装、配置、测试

    MySQL安装.配置.测试(win7_64bit) 目录 1.概述 2.本文用到的工具 3.MySQL安装配置 4.Java访问MySQL测试 5.注事事项 6.相关博文 >>看不清的图片 ...