这是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. Zend Studio集成Xdebug断点调试详解

    转自:http://www.softown.cn/post/115.html Xdebug是PHP开发中两个常用的断点调试工具之一(另一个为Zend Debugger). 现在,我们在Zend Stu ...

  2. drivers/mfd/Mfd-core.c

    mfd: multifunction device drivers---多功能设备驱动开发:A product or device that has multiple functions. An ex ...

  3. [Python]如何使用HtmlTestRunner让自动化测试报告内容更丰富

    简述 使用selenium webdriver + Python做自动化测试,执行完成后要生成测试报告,Python我们使用的HTMLtestrunner 进行生成,但是默认提供的生成报告内容,并不能 ...

  4. 2016年6月24日 星期五 --出埃及记 Exodus 14:21

    2016年6月24日 星期五 --出埃及记 Exodus 14:21 Then Moses stretched out his hand over the sea, and all that nigh ...

  5. linux 开启wifi热点

    1,在网络连接管理中创建一个wifi连接,点击 Add,然后选Wi-Fi 2,设置wifi热点名字.wifi接连名字 3,设置 Mode 选 Ad-hoc,其它默认. 4,在 Wi-Fi Securi ...

  6. SqlSever基础 dateadd month 增加五个月

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

  7. UVA 11404 五 Palindromic Subsequence

     Palindromic Subsequence Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu ...

  8. CodeForces 373B Making Sequences is Fun

    Making Sequences is Fun Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  9. SQL判断汉字

    /* unicode编码范围: 汉字:[0x4e00,0x9fa5](或十进制[19968,40869]) 数字:[0x30,0x39](或十进制[48, 57]) 小写字母:[0x61,0x7a]( ...

  10. Example of Get_File_Name Function in Oracle Forms

    Displays the standard open file dialog box where the user can select an existing file or specify a n ...