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 ...
随机推荐
- python ctypes小例子
import time import ctypes import ctypes.wintypes SEE_MASK_NOCLOSEPROCESS = 0x00000040 SEE_MASK_INVOK ...
- [Angular 2] Using events and refs
This lesson shows you how set listen for click events using the (click) syntax. It also covers getti ...
- shell读取文件参数
环境 csh 说明 通常我们需要使用使用shell脚本处理一些事务,每次调用shell都需要添加参数. 如果重复调用多次这个shell脚本,我们可以将参数存入指定文件,循环得到参数. shell脚本( ...
- MySql查看表信息
SELECT TABLE_NAME, TABLE_COMMENT -- 指定信息列 FROM `information_schema`.`tables` A WHERE A.`TABLE_SCHEMA ...
- 使用date转换UNIX时间戳
1.将time string转换成时间戳 date -d "2010-10-12 12:25:00" +%s 2.将时间戳转换成time string date -d " ...
- SQL获取选中时间的交集
如上图:t1,t2代表要选择的时间段,t3,t4代表系统时间. 那么如果要获取选中时间段所有的交集为: 条件1 and ((t3>t1 and t1>t2) or (t3<t2 an ...
- iOS_SN_Socket - AsyncSocket
转载文章,原地址:http://yimouleng.com/2015/02/04/Socket-AsyncSocket/ 一.前言 公司的项目用到了Socket编程,之前在学习的过程当中,用到的更多的 ...
- xampp集成安装的mysql修改密码(Window)
把mysql安装目录bin文件夹加入环境变量 path:mysqlPath\bin; 或者进入mysql安装目录bin文件夹下,按住shift键盘鼠标右击进入命令行 键入命令 mysqladmin - ...
- 3 linux、windows环境---路径分隔符不同导致的问题
问题:通常在eclipse,IntelliJ IDEA等进行代码编写时,程序中用到路径通常采用/job/test.properties或D:/job/test.properties等是形式作为文件路径 ...
- The Suspects(POJ 1611 并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30158 Accepted: 14665 De ...