Adapter可以视作控件与数据之间的桥梁

对ListView做自由布局和填充需要使用到Adapter,这里我们采用SimpleAdapter。

简单来说:

1.定义一个ListItem,其数据结构是一个元素为HashMap的ArrayList。

2.填充ListItem

3.使用一个SimpleAdapter将ListItem与Item.xml关联起来

4.将ListView与SimpleAdapter关联起来

逻辑关系用VISO表示如下:

下面是代码:

MainActivity.java

public class MainActivity extends ActionBarActivity {

    ListView list;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.list1); ArrayList<HashMap<String , Object>> listItem = new ArrayList<HashMap<String, Object>>();
for(int i=0;i<10;i++){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("ItemImage",R.drawable.icon);
map.put("ItemTitle",i+"row");
listItem.add(map);
}
SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,R.layout.item,
new String[]{"ItemImage","ItemTitle","ItemText"},
new int[]{R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});
list.setAdapter(mSimpleAdapter);
}
}

Item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<ImageView
android:layout_alignParentLeft="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ItemImage"/>
<TextView
android:id="@+id/ItemTitle"
android:layout_toRightOf="@+id/ItemImage"
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:textSize="20sp"/>
<TextView
android:id="@+id/ItemText" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_below="@+id/ItemTitle"/>
</RelativeLayout>

ListView与SimpleAdapter的更多相关文章

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

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

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

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

  3. ListView 搭配SimpleAdapter

    这是SimplerAdapter的构造函数 public SimpleAdapter(Context context, List<? extends Map<String, ?>&g ...

  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. [转]how to use both JDK 7 and JDK 8 in one build

    Note: This article is original from https://gist.github.com/aslakknutsen/9648594 JDK 8 Released Most ...

  2. Web请求过程总结

    Web请求过程总结 1.CND架构图 图片来源:深入分析JavaWeb技术内幕(许令波著) 2.发起HTTP请求 发起一个HTTP请求就是浏览器建立socket通信的过程,HttpClient开源的通 ...

  3. go test遇到的一些问题-command-line-arguments undefined: xxxxx

    一 问题是在我写算法题的时候出的,test后缀的文件编译报command-line-arguments undefined: xxxxx 二 没记错,go test是 所有在以_test结尾的源码内以 ...

  4. 深入理解JavaScript系列(32):设计模式之观察者模式

    介绍 观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态发生变化时就会通知所有的观察者对象,使得它们 ...

  5. js判断下拉框改变状态

    <script> $('#questSort').change(function(){ //此处写状态改变要实现的功能 var s=$('#questSort').children('op ...

  6. mvc路由引起异步调用web服务的问题

    从一篇blog得知使用脚本可以异步调用Web服务,觉得很新鲜,因为自己很少用到Web服务,所以决定写一写看看什么效果. 首先在UI项目(我使用的是MVC4.0)里创建一个Web服务. 添加Web服务后 ...

  7. 资源:开源Fuzzers工具列表 (以及其它fuzzing工具)

    开源fuzzers‍ / 开源fuzzing工具的最新列表(Fuzzers,没有标准中文翻译,可以理解为模糊测试工具或者模糊器) 如果你知道有需要添加的部分,那么请在这里或在推特上@Peerlyst来 ...

  8. 2.浅析Hadoop之YARN

    YARN也是主从架构,主节点是ResourceManager,从节点是NodeManager,是一种资源分配及任务管理的组件 针对每个任务还有ApplicationMaster应用管理者和Contai ...

  9. (转)原生ajax的写法

    1.创建XMLHttpRequest对象 function createXMLHTTPRequest() { //1.创建XMLHttpRequest对象 //这是XMLHttpReuquest对象无 ...

  10. Daily paper -Science 2006: Experimental Study of Inequality and Unpredictability in an Artificial Cultural Market (探究群体行为对商品销量的影响)

    论文:      Experimental Study of Inequality and Unpredictability in an Artificial Cultural Market 发表期刊 ...