这是SimplerAdapter的构造函数

public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
@LayoutRes int resource, String[] from, @IdRes int[] to) {
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
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.

context:是上下文;

List<?extends Map<String,?>> data:这里是listview的数据来源,最外层是一个list数据结构list泛型指定的是继承了Map的对象。这里准备使用ArrayList<HashMap<String,String>>;

resource:这是listview的布局文件注意着是@LayoutRes那么就应该是R.layout里的xml文件

from:看注释这里是指定数据源中Map的column的名字,也就是说是指定Map<key,value>中key;是我们添加数据的索引。

to:这是需要显示的TextView(这里必须是TextView)的id,对应的是Map<key,value>中value,将是我们可见的内容。

那么ListView的布局如下:hashmap_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hash_title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/hash_aticle"/>
</LinearLayout>

主要代码如下

 private ArrayList<HashMap<String,String>> item;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.my_listview);
DataInit();
SimpleAdapter sa = new SimpleAdapter(this,item,R.layout.hashmap_item,new String[]{"title","article"},new int[]{R.id.hash_title,R.id.hash_aticle});
listView.setAdapter(sa);
} private void DataInit(){
item = new ArrayList<>(20);
for(int i=0;i<30;i++){
HashMap<String,String> hashmap = new HashMap<>();
hashmap.put("title","title:"+i);
hashmap.put("article","article:"+i);
item.add(hashmap);
}
}

activity_main.xml定义如下

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.skymaster.hs.arrayadapter.MainActivity"> <ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_listview" />
</RelativeLayout>

程序效果图

ListView 搭配SimpleAdapter的更多相关文章

  1. ListView与SimpleAdapter

    Adapter可以视作控件与数据之间的桥梁 对ListView做自由布局和填充需要使用到Adapter,这里我们采用SimpleAdapter. 简单来说: 1.定义一个ListItem,其数据结构是 ...

  2. 滚动视图、列表视图[ListView、SimpleAdapter类]

    滚动视图 <ScrollView android: layout_width="fill_parent" android: layout_height="fill_ ...

  3. 安卓开发_浅谈ListView(SimpleAdapter数组适配器)

    安卓开发_浅谈ListView(ArrayAdapter数组适配器) 学习使用ListView组件和SimapleAdapter适配器实现一个带图标的ListView列表 总共3部分 一.MainAc ...

  4. ListView之SimpleAdapter

    SimpleAdapter是安卓内置的适配器,本文展示的是listview的子项为{图片,文件}组合 如下图所示: 具体代码: SimpleAdapter_test.java /* ListView ...

  5. AdapterView及其子类之四:基于ListView及SimpleAdapter实现列表

    代码请见SimpleAdapterDemo.zip. 步骤如下: 1.创建主布局文件 <RelativeLayout xmlns:android="http://schemas.and ...

  6. Android ListView 之 SimpleAdapter 二 (包含 item 中按钮监听)

    1    MainActivity.java package com.myadapter; import java.util.ArrayList; import java.util.HashMap; ...

  7. ListView与SimpleAdapter(三)

    一般用于只有两个控件的列表. 使用SimpleAdapter 的数据是以List<Map<String,?>>形式封装数据, List的每一节对应ListView的每一行. H ...

  8. 关于SimpleAdapter和ListView结合使用,实现列表视图的笔记

    使用ListView需要为其添加适配器: 适配器有两种:1.ArrayAdapter  --用于单独文字显示 2.SimpleAdapter --用于文字和图片显示 这里主要记录SimpleAdapt ...

  9. Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定

    1.listview的数据填充可以通过ArrayAdapter,SimpleAdapter,也可以是一个xml文件,但是ArrayAdapter与SimpleAdapter的区别是: 2 ArrayA ...

随机推荐

  1. AutoIt实现selenium上传文件

    1,安装autoIt 2,工程中建文件夹,例如:script,将autoit 复制到该文件夹,并且编辑视频所在的文件夹的路径,编辑"au3"格式的文件 3,编辑完成后生成EXE文件 ...

  2. java反编译工具JD-GUI

    这款java反编译工具是由C++写的,是一款免费的非商业用途的软件,(Xjad也不错,但是不支持jar反编译) 一.支持众多.class反编译工具 二.支持反编译jar

  3. (2)redis的基本数据结构是动态数组

    redis的基本数据结构是动态数组 一.c语言动态数组 先看下一般的动态数组结构 struct MyData { int nLen; ]; }; 这是个广泛使用的常见技巧,常用来构成缓冲区.比起指针, ...

  4. 利用JAVA Service Wrapper把JAVA程序做成windows服务

    今天做了一个读取数据入库的程序.由于读取的数据每天都更新,于是就想把程序做成一个服务,每天定时执行.研究了一下,发现有几种方式可以做.下面我主要记录一下JAVA Service Wrapper方式. ...

  5. CodeForces 670D2 Magic Powder 二分

    D2. Magic Powder - 2 The term of this problem is the same as the previous one, the only exception — ...

  6. CentOS6.6系统源代码安装mysql5.5.28教程(附源码包下载地址)+sysbench的安装

    mysql从5.5版本开始,不再使用./configure编译,而是使用cmake编译器,具体的cmake编译参数可以参考mysql官网文档(※ 非常重要) http://dev.mysql.com/ ...

  7. SqlSever基础 rtrim函数 除去字符串的右边的空格,左边中间的不管

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  8. Java _ JDK _ Arrays, LinkedList, ArrayList, Vector 及Stack

    (最近在看JDK源码,只是拿着它的继承图在看,但很多东西不记录仍然印象不深,所以开始记录JDK阅读系列.) (一)Arrays Arrays比较特殊,直接继承自Arrays ->List(Int ...

  9. POSTMAN中各种请求方式的区别

    1.form-data:  就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件 ...

  10. PHPwebshell分析

    这几天已知在做webshell的检测,JSP的不说了,特征检测起来很好匹配到,而且全是一家亲,互相模仿的居多. 今天看了一篇文章,看到14年蘑菇的webshell的后门然后很不错. ========= ...