ListView之SimpleAdapter
SimpleAdapter是安卓内置的适配器,本文展示的是listview的子项为{图片,文件}组合
如下图所示:

具体代码:
SimpleAdapter_test.java
/*
ListView :列表
通常有两个职责:
a.将数据填充到布局
b.处理点击事件 一个ListView创建需要几个元素:
a.ListView中第一列的 View
b.填入View的图片或数据
c.连接数据 与ListView的适配器 有哪些适配器?
ArrayAdapter<T> 用来绑定一个数组,支持泛型设计
SimpleAdapter 用来绑定在xml中定义的控件和对应的数据
SimpleCursorAdapter:用来绑定游标得到的数据
BaseAdapter 通用的基础适配器 *
* */
public class SimpleAdapter_test extends Activity { private ListView listview;
private int[] ids=new int[]{
R.drawable.s1,
R.drawable.s2,
R.drawable.s3,
R.drawable.s4,
R.drawable.s5}; private SimpleAdapter adapter;
private Context context;
private List<Map<String,Object>> datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baseadapate);
context = this;
listview = (ListView) findViewById(R.id.listview); initData();
//map中所有的key的
String[] from=new String[]{"map_image","map_content"};
int[] to=new int[]{R.id.image,R.id.content};
adapter=new SimpleAdapter(context, datas, R.layout.items2, from, to); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) { Toast.makeText(context,"你选中的是:"+ datas.get(position).get("map_content"), ).show();
}
}); } private void initData() { datas = new ArrayList<Map<String,Object>>();
for(int i=;i<;i++)
{
Map<String,Object> map = new HashMap<String, Object>();
map.put("map_image",BitmapFactory.decodeResource(getResources(), ids[i]));
map.put("map_content", "hahacontent"+i);
datas.add(map);
}
} }
baseadapate.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="match_parent"
android:orientation="vertical" > <ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview"
> </ListView> </LinearLayout>
items2.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="match_parent"
android:orientation="horizontal"
android:layout_gravity="center" > <ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
/> <TextView
android:layout_marginTop="15dp"
android:id="@+id/content"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight=""
android:text="haa" /> </LinearLayout>
不要忘了在清单里注册activity,并且设置为app入口
<activity android:name=".BaseAdapter_test">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
ListView之SimpleAdapter的更多相关文章
- ListView与SimpleAdapter
Adapter可以视作控件与数据之间的桥梁 对ListView做自由布局和填充需要使用到Adapter,这里我们采用SimpleAdapter. 简单来说: 1.定义一个ListItem,其数据结构是 ...
- 滚动视图、列表视图[ListView、SimpleAdapter类]
滚动视图 <ScrollView android: layout_width="fill_parent" android: layout_height="fill_ ...
- 安卓开发_浅谈ListView(SimpleAdapter数组适配器)
安卓开发_浅谈ListView(ArrayAdapter数组适配器) 学习使用ListView组件和SimapleAdapter适配器实现一个带图标的ListView列表 总共3部分 一.MainAc ...
- ListView 搭配SimpleAdapter
这是SimplerAdapter的构造函数 public SimpleAdapter(Context context, List<? extends Map<String, ?>&g ...
- AdapterView及其子类之四:基于ListView及SimpleAdapter实现列表
代码请见SimpleAdapterDemo.zip. 步骤如下: 1.创建主布局文件 <RelativeLayout xmlns:android="http://schemas.and ...
- Android ListView 之 SimpleAdapter 二 (包含 item 中按钮监听)
1 MainActivity.java package com.myadapter; import java.util.ArrayList; import java.util.HashMap; ...
- ListView与SimpleAdapter(三)
一般用于只有两个控件的列表. 使用SimpleAdapter 的数据是以List<Map<String,?>>形式封装数据, List的每一节对应ListView的每一行. H ...
- 关于SimpleAdapter和ListView结合使用,实现列表视图的笔记
使用ListView需要为其添加适配器: 适配器有两种:1.ArrayAdapter --用于单独文字显示 2.SimpleAdapter --用于文字和图片显示 这里主要记录SimpleAdapt ...
- Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定
1.listview的数据填充可以通过ArrayAdapter,SimpleAdapter,也可以是一个xml文件,但是ArrayAdapter与SimpleAdapter的区别是: 2 ArrayA ...
随机推荐
- 详细介绍如何使用kindEditor编辑器
今天群里的朋友问我能不能写个kindEditor编辑器的使用教程,说是弄了半天没有搞定.由于PHP啦后台正好用了这个编辑器,我有写经验,正好教他的同时写出来分享给大家. kindEditor编辑器是一 ...
- SecureCRT 安装及初始化配置
安装 SecureCRT 7.3.4 安装以及破解方法 SecureCRT 6.5.0 汉化解压版 初始化配置 这里配置以SecureCRT 6.5.0 汉化解压版为例 1.调整SecureCRT终端 ...
- 基于Cordova5.0开发自己定义插件(android)
1.开发插件java部分 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenhmMjE2MjE2/font/5a6L5L2T/fontsize/400/fi ...
- JavaScripts学习日记——ECMAscript
1.Function对象 Function是一个很特殊的对象,特殊在该对象就像java中的方法一样,可以运行,可以传参数. 三种定义function对象的方法: 1.function fun1(a,b ...
- Hacker(21)----密码攻防之加密与解密基础
密码对于用户而言并不陌生,它是一种用于保护重要信息和文件的工具,只有输入正确的密码才可查看文件和信息的具体内容.黑客为了获取这些信息,会采用各种方式来破解密码,因此用户不仅需要了解黑客破解密码的常用方 ...
- 普通用户之间的ssh无密码访问设置方法
两台CentOS6.2服务器,客户端是node1,服务器是node2,先都用root用户配置,方法如下: 第一步:在客户端Node1:生成密匙对,我用的是rsa的密钥.使用命令 "ssh-k ...
- TravelCMS旅游网站系统诞生记-1(后台框架篇)
- UIAlertController的创建以及添加
个人还是更喜欢以前的UIAlertView的创建方法,更新后的UIAlertController虽然说将UIAlertView和UIActionSheet的功能和作用以一种模块化替换的方式来代替,但是 ...
- IIS报500.0错误
IIS安全里面配置:Everyone.IUSR.IIS_IUSRS 参考地址:http://blog.chinaunix.net/uid-21375345-id-3213631.html
- 关于毕设WiFi选型
毕设做基于WiFi的家庭灯光影音控制,现在主要是WiFi模块上的选型,自己选了ESP8266. 一开始是看到了机智云的内容,想尝试机智云.但是目前没有母版,不知道怎么下手. 接下来就先尝试通过通用类型 ...