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 ... 
随机推荐
- ubuntu14.04 + cocos2d-x-2.2.6 + eclipse发布android + Qt Creator4
			先把需要的东西准备好,打开控制台,执行以下语句: sudo apt--jdk lib32z1 lib32ncurses5 lib32bz2- 接下来,准备好cocos2d-x-2.2.6和 andro ... 
- js获取url传递参数(转的,原作不详)
			这里是一个获取URL带QUESTRING参数的JAVASCRIPT客户端解决方案,相当于asp的request.querystring,PHP的$_GET 函数: <Script languag ... 
- mysql学习之五:sql语句学习3
			好吧,大家认为这样的字体还是比較好看,全部我们就换这样的字体了. INSERT INTO 语句用于向表格中插入新的行. 语法 INSERT INTO 表名称 VALUES (值1, 值2,....) ... 
- [IOI1999]花店橱窗布置(DP路径记录)
			题目:[IOI1999]花店橱窗布置 问题编号:496 题目描述 某花店现有F束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V ... 
- 让你不再纠结GitHub:Git起步
			一.关于版本控制 版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情况的系统.我们通常仅对保存着软件源代码的文本文件做版本控制,但实际上,你可以对任何类型的文件进行版本控制. 采用版本控制 ... 
- java基础之成员变量与局部变量
			成员变量的含义 局部变量的含义 成员变量与局部变量的区别 
- (转)用来理解Java的8个图表
			很多时候,一张图比你说 1000 个字能更有效的说清楚一个问题.我们列举了 8 个关于 Java 语言的图表,或许可以让你对 Java 有着更深入的认识. 1.字符串不变性(String Immuta ... 
- (转)union和union all的区别
			Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All 两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个s ... 
- win7_64位主机装虚机Linux系统(VMware Workstation10+CentOS6.5)详细步骤图文讲解
			第一步:创建新的虚拟机 第二步:选择“典型”安装 第三步:选择映像文件安装—浏览选择iso文件 第四步:选择稍后安装操作系统 第五步:系统选择Linux,版本选择centOS64位 第六步:虚拟机名称 ... 
- #JavaScript对象与继承
			JavaScript对象与继承 JavaScript是我在C语言之后接触的第二门编程语言,大一暑假的时候在图书馆找了一本中国人写的JavaScript程序设计来看.那个时候在编程方面几乎还是小白,再加 ... 
