本节主要说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. 【Android】Android 中string-array的用法

    在Android中,用string-array是一种简单的提取XML资源文件数据的方法. 例子如下: 把相应的数据放到values文件夹的arrays.xml文件里 <?xml version= ...

  2. zprofiler三板斧解决cpu占用率过高问题(转载)

    zprofiler三板斧解决cpu占用率过高问题 九居 JVM性能与调试平台   zprofiler   上周五碰到了一个线上机器cpu占用率过高的问题.问题本身比较简单,但是定位过程中动用了多个zp ...

  3. sparkStreaming消费kafka-1.0.1方式:direct方式(存储offset到zookeeper)

    版本声明: kafka:1.0.1 spark:2.1.0 注意:在使用过程中可能会出现servlet版本不兼容的问题,因此在导入maven的pom文件的时候,需要做适当的排除操作 <?xml ...

  4. net core体系-web应用程序-1VS2017构建一个简单的web

    使用vs2017,添加一个新项目-asp.net core web应用程序. 结构如图, wwwroot放了网站的静态资源如css.js.image文件: appsetting.json是应用程序的配 ...

  5. Python学习(三十)—— Django框架简介

    转载自:http://www.cnblogs.com/liwenzhou/p/8296964.html Django框架简介 一.MVC框架和MTV框架(了解即可) MVC,全名是Model View ...

  6. 用easy-ui中的表单操作实现一行操作和多行操作

    http://blog.sina.com.cn/s/blog_8e50ede90101fff9.html

  7. TensorFlow激活函数+归一化-函数

    激活函数的作用如下-引用<TensorFlow实践>: 这些函数与其他层的输出联合使用可以生成特征图.他们用于对某些运算的结果进行平滑或者微分.其目标是为神经网络引入非线性.曲线能够刻画出 ...

  8. Bi-shoe and Phi-shoe (欧拉函数)

    题目描述: 题目大意:一个竹竿长度为p,它的score值就是比p长度小且与且与p互质的数字总数,比如9有1,2,4,5,7,8这六个数那它的score就是6.给你T组数据,每组n个学生,每个学生都有一 ...

  9. python3 在 windows 读取路径多了一个\u202a 是咋回

    python3 在 windows 读取路径多了一个\u202a 是咋回事

  10. BZOJ-3-1010: [HNOI2008]玩具装箱toy-斜率优化DP

    dp[i]=min(dp[j]+(sum[i]-sum[j]+i-j-1-L)^2) (j<i) 令f[i]=sum[i]+i,c=1+l 则dp[i]=min(dp[j]+(f[i]-f[j] ...