本节主要说Collection的字符串数组、List、SparseArray、Map的绑定。先看看xml的布局。

 <layout xmlns:android="http://schemas.android.com/apk/res/android">

     <data class="CollectionsBinding">
<import type="java.util.Map" />
<import type="java.util.List" />
<import type="android.util.SparseArray" /> <variable name="list" type="List&lt;String>" />
<variable name="sparse" type="SparseArray&lt;String>"/>
<variable name="map" type="Map&lt;String, String>"/>
<variable name="index" type="int" />
<variable name="key" type="String" />
</data> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="list[index]:" />
<TextView
android:text="@{list[index]}"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="sparse[index]:" />
<TextView
android:text="@{sparse[index]}"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="map[key]:" />
<TextView
android:text="@{map[key]}"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <View
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" /> <TextView
android:text='map["firstName"]'
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text='@{map["firstName"]}'
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:text="map[`firstName`]"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@{map[`firstName`]}"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:text="map[&amp;quot;firstName&amp;quot;]"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@{map[&quot;firstName&quot;]}"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout> </layout>

首先在<data>导入Collection的类型。&lt;是< 的转义符。获取值的时候使用@{ map[key] }

在binding设置值。

binding.setIndex(index);
binding.setKey(key);
binding.setList(list);
binding.setSparse(sparse);
binding.setMap(map);

本节参考代码。

Android的Databinding-数据、Map绑定的更多相关文章

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

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

  2. AngularJS中数据双向绑定(two-way data-binding)

    1.切换工作目录 git checkout step-4 #切换分支,切换到第4步 npm start #启动项目 2.代码 app/index.html Search: <input ng-m ...

  3. wemall app商城源码Android 获取XML网络数据并绑定到ListView

    wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享Android 获取XML网络数据并绑定到Li ...

  4. android实现json数据的解析和把数据转换成json格式的字符串

    利用android sdk里面的 JSONObject和JSONArray把集合或者普通数据,转换成json格式的字符串 JSONObject和JSONArray解析json格式的字符串为集合或者一般 ...

  5. Android中的数据存储

    Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...

  6. Android 里的数据储存

    数据持久化 关于数据储存,这个话题已经被反复讨论过很多次了,我是不建议把网络存储这种方式纳入到数据储存的范围的,因为这个和Android没多少关系,因此就有如下的分类: 本地储存(也称之为数据持久化, ...

  7. Android数据绑定DataBinding(二)入门篇

    前言 之前写了Android数据绑定DataBinding(一)入门篇,很简单的记录了如何使用DataBinding,其初衷是想要代码中的数据发生改变,不需要繁琐的setText等操作,在最后说到了只 ...

  8. Android中Service通信(二)——绑定Service进行通信

    一.把输入文本的数据同步到服务的实例(如何执行服务的内部代码) 绑定服务比启动服务更加方便高效,绑定服务中的直接方法调用比Intent作为载体传输更为快捷得多. 1.activity_main.xml ...

  9. 我的angularjs源码学习之旅3——脏检测与数据双向绑定

    前言 为了后面描述方便,我们将保存模块的对象modules叫做模块缓存.我们跟踪的例子如下 <div ng-app="myApp" ng-controller='myCtrl ...

  10. android bundle存放数据详解

    转载自:android bundle存放数据详解 正如大家所知道,Activity之间传递数据,是将数据存放在Intent或者Bundle中 例如: 将数据存放倒Intent中传递: 将数据放到Bun ...

随机推荐

  1. Python_os模块

    os模块:可以处理文件和目录,是Python系统和操作系统进行交互的一个接口 os模块常用方法: os.getcwd(): 获取当前工作目录,(即当前Python脚本工作的目录路径) os.chdir ...

  2. eclipse启动web应用 报错

    错误:The origin server did not find a current representation for the target resource or is not willing ...

  3. ubuntu镜像下载地址

    下载地址: http://mirror.pnl.gov/releases/xenial/ Ubuntu 14.04.5 LTS (Trusty Tahr)http://releases.ubuntu. ...

  4. Codeforces 980F Cactus to Tree 仙人掌 Tarjan 树形dp 单调队列

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF980F.html 题目传送门 - CF980F 题意 给定一个 $n$ 个节点 $m$ 条长为 $1$ 的边 ...

  5. {}动态规划}记忆化dp

    先搞个模板 #include<stdio.h> #include<string.h> using namespace std; typedef long long ll; ]; ...

  6. 003 使用SpringMVC开发restful API--查询用户

    一:介绍说明 1.介绍 2.restful api的成熟度 二:编写Restful API的测试用例 1.引入spring的测试框架 在effective pom中查找 2.新建测试包,测试类 3.测 ...

  7. 027 ResourceBundle.getBundle方法

    在程序中遇到这个,感觉会比较重要,就学习一番. 一:静态读取配置文件的Demo 1.新建工程 2.新建properties 3.新建Java的demo程序 注意不要写properties. packa ...

  8. context-param和init-param的区别

    http://www.cnblogs.com/hzj-/articles/1689836.html

  9. XX-NET史上最详细完整教程

     转 https://www.cnblogs.com/phperkang/p/8780123.html 前言 XX-NET,系GAE类代理,即通过可用Google ip连接Google App Eng ...

  10. P2415 集合求和

    P2415 集合求和显然,一共有2^n个子集,对于其中的一个确定的元素,它不在的集合有2^(n-1),相当于有n-1元素,那么它存在的集合有,2^n-2^(n-1)==2^(n-1),那么集合的和为s ...