MainActivity:

package com.wyl.listview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity {
ListView listview;
ListAdapter adapter;
ListAdapter la;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init(); }
private void init() {
listview = (ListView) findViewById(R.id.listview01);
//// adapter.a
// //数据源,
// //1.新建适配器
// //
// String[] a = {"the first","the second","第三个","张雅岚"};
//// List<String> b = new ArrayList<String>();
//// b.add("张雅岚");
//// b.add("wyl");
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, a);
// listview.setAdapter(adapter); //
// String[] c = {"one","two","three","four"};
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 , c);
// System.out.println("=============");
// listview.setAdapter(adapter);
// la = new SimpleAdapter(this, getData(), resource, from, to) //
// public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
//
// Added in API level 1
// Constructor
//
// Parameters
// context The context where the View associated with this SimpleAdapter is running
// data A List of Maps. Each entry in the List corresponds to one row in the list.
// The Maps contain the data for each row, and should include all the entries specified in "from"
// resource Resource identifier of a view layout that defines the views for this list item. The layout
// file should include at least those named views defined in "to"
// from A list of column names that will be added to the Map associated with each item.
// to The views that should display column in the "from" parameter. These should all be TextViews.
// The first N views in this list are given the values of the first N columns in the from parameter. la = new SimpleAdapter(this,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text}); listview.setAdapter(la); }
private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map map = new HashMap<String,Object>();
for(int i=0;i<10;i++){
map.put("pic", R.drawable.ic_launcher);
map.put("text", "wyl"+i);
System.out.println("i:"+i);
list.add(map); } return list; } }

  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" >
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="20sp"
android:textColor="#000000"
android:text="demo"
/>
</LinearLayout>

  PS:SimpleAdapter的构造方法的参数解释:

new SimpleAdapter(context,data,resource,from,to)
context: 上下文
data: 数据源(List<? extends Map<String,?>>data) 一个Map所组成的List集合
每一个Map都会去对应ListView列表中的一行
每一个Map(键 - 值对)中的键必须包含所有在from中所指定的值
resource: 列表项的布局文件ID
from: Map中的键名
to: 绑定数据视图中的ID,与from成对应关系
-----------------------------------
context:一般指当前的activity,用this即可;
data:一个数据源,本课程的重点,创建Map并添加信息;
resource:一个布局文件ID,表示listview中每一个item的布局方式;
from:Map的键名,listview中的item内部元素对应的名字;
to:resource布局文件中,每个元素的ID。

这里说说我自己对这这五个参数的理解。

我们可以发现,第三个参数和第五个参数都跟所要显示出来的具体有关,如左侧显示图片,右侧显示备注。可能我们会想,为什么

已经有了第五个参数,即那个图片和备注的R.id.xx属性的数组,那为什么还要再加上第三个参数resouce呢?有了更具体的的R.id.xx属性

那么我们就能够知道这个数组所表示的属性所在的layout布局了,也就完全没必要再给出第二个参数了。其实问题就出在这里,因为第五个

参数是一个int 类型的数组,在安卓系统根不知道这些数字到底是对应着文字或者图片。如果没有第三个参数,那么android就不知道本例

中第三个参数数组中的下标为0的对应的是图片,那么系统只能够根据第二个参数绑定的值,即

map.put("pic", R.drawable.ic_launcher);

  这行代码中的R.drawable.ic_launcher,这个值实际上是一个16进制数,在gen.具体包名.R.java中的

public static final class drawable {
public static final int ic_launcher=0x7f020000;
public static final int yl=0x7f020001;
}

 这行代码中的0x7f020000这个16进制数,对应的10进制为:2130837504,那么运行这个安卓应用时显示的不是图片,而这个10进制

数:2130837504。

由此:我们要特别注意:第3,4,5个参数一一对应。如果不对应起来可能发生意想不到的结果。

有个具体的例子的图如下:

Android:ListViewAdapter的更多相关文章

  1. Android 多个listview的实现

    正好,今天项目中需要,先写了个demo,给大家参考参考. 先上图,需要的自己,看看具体的代码实现步骤 大概说一下实现步骤: 1.布局中先用 scrollview 包裹 LinearLayout < ...

  2. Android中ListView动态加载数据

    1. 引言: 为了提高ListView的效率和应用程序的性能,在Android应用程序中不应该一次性加载ListView所要显示的全部信息,而是采取分批加载策略,随着用户的滑动,动态的从后台加载所需的 ...

  3. Android中ListView错位布局实现(无聊向)

    由于某些原因,需要个错位的页面,在网上找不到好的例子,试着动手写了写. 不考虑配色的完成图如下: 首先考虑的是,listview每一行左右都有可能缩进. 先假设一行的布局就是ImageView,Tex ...

  4. android代码优化----ListView中自定义adapter的封装(ListView的模板写法)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  5. 【转】Android之自定义Adapter的ListView

    http://www.cnblogs.com/topcoderliu/archive/2011/05/07/2039862.html 在开发中,我们经常使用到ListView这个控件.Android的 ...

  6. android之下拉刷新(reflush)

    package com.example.reflush; import android.app.ListActivity; import android.os.Bundle; import andro ...

  7. Android代码优化----PullToRefresh+universal-image-loader实现从网络获取数据并刷新

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  8. Android ListView 自定义 Adapter

    自定义Adapter类 public class ListViewAdapter extends BaseAdapter { private static final String TAG = Mai ...

  9. Android批量图片加载经典系列——采用二级缓存、异步加载网络图片

    一.问题描述 Android应用中经常涉及从网络中加载大量图片,为提升加载速度和效率,减少网络流量都会采用二级缓存和异步加载机制,所谓二级缓存就是通过先从内存中获取.再从文件中获取,最后才会访问网络. ...

随机推荐

  1. 射频识别技术漫谈(27)——CPU卡概述

    智能卡按安全级别可以分为三类:存储器卡.逻辑加密卡和CPU卡,其中CPU卡是安全级别最高的.从“CPU”这个名字可以看出,CPU卡最大的特点就是卡片里面有一个"CPU",有了CPU ...

  2. Qt开发小工具之gif转换器(使用QMovie截取每一帧为QImage,然后用QFile另存为图片文件)

    最近,QQ上好多各种gif表情.每一个都很经典呀..于是我就想把它转换成一张张静态图片...没学过ps.于是写了几行代码.完工.核心代码如下 主要是借助QMovie类.文件读取模式选择QMovie:: ...

  3. 使用Win32 API创建不规则形状&带透明色的窗口

    前一阵突然想起了9月份电面某公司实习时的二面题,大概就是说怎么用Win32 API实现一个透明的窗口,估计当时我的脑残答案肯定让面试官哭笑不得吧.所以本人决定好好研究下这个问题.经过一下午的摸索,基本 ...

  4. rpc的学习

    rpc(Remote process call 即远程过程调用)是一种请求-相应的协议, 主要使用于C/S架构中,使得分布式系统成为可能.由客户端发起请求,服务端调用各种参数处理请求,当服务器在处理请 ...

  5. Baby Step Gaint Step

    给定同余式,求它在内的所有解,其中总是素数. 分析:解本同余式的步骤如下 (1)求模的一个原根 (2)利用Baby Step Giant Step求出一个,使得,因为为素数,所以有唯一解. (3)设, ...

  6. 用elasticsearch索引mongodb数据

    参照网页:单机搭建elasticsearch和mongodb的river 三个步骤: 一,搭建单机replicSet二,安装mongodb-river插件三,创建meta,验证使用 第一步,搭建单机m ...

  7. express文件上传

    安装express,创建项目,添加sqlite3模块 express --sessions --css stylus --ejs myhotel npm install sqlite3node app ...

  8. cordova安装--创建ionic项目

    1.简介ionic ionic 是一个强大的 HTML5 应用程序开发框架(HTML5 Hybrid Mobile App Framework ). 可以帮助您使用 Web 技术,比如 HTML.CS ...

  9. [译]Stairway to Integration Services Level 18 – 部署和执行

    介绍 在本文中,我们要创建一个SSIS Catalog 实例,部署我们的项目,并且运行 weather data loader 包. SSIS 2012 部署模型   SSIS 2012 Deploy ...

  10. python安装zlib一直无效

    一直按网上的方法: 1.先安装 apt-get install zlib1g-dev 2.重新安装python(3.3):即是./configure 再make再make install 始终没有解决 ...