在编写Android应用的时候经常需要做的事情就是对View的数据进行设置,在Android下设置控件相对.net来说是件麻烦的事情,首先根据ID从view把控件找出来然后才能设置相应属性值;如果数据成员多那这些工作的是繁锁的事情。下面通过java提供的reflect的功能实现数据自动绑定功能。

在实现之前先描述一下实现的功能效果。

传统方式:

 EditText editor = (EditText)v.findViewById(R.id.orders_orderid);
editor.setText(item.getOrderID());
editor =(EditText)v.findViewById(R.id.orders_employee);
editor.setText(item.getEmployee());
editor=(EditText)v.findViewById(R.id.orders_customer);
editor.setText(item.getCustomer());
editor =(EditText)v.findViewById(R.id.orders_orderdate);
editor.setText(item.getOrderDate());
editor =(EditText)v.findViewById(R.id.orders_requireddate);
editor.setText(item.getRequiredDate());
editor=(EditText)v.findViewById(R.id.orders_shipaddress);
editor.setText(item.getShipAddress());
editor =(EditText)v.findViewById(R.id.orders_shipcity);
editor.setText(item.getShipCity());
editor=(EditText)v.findViewById(R.id.orders_shipname);
editor.setText(item.getShipName());
editor =(EditText)v.findViewById(R.id.orders_shippedDate);
editor.setText(item.getShippedDate());
editor =(EditText)v.findViewById(R.id.orders_shipregion);
editor.setText(item.getShipRegion());

数据绑定方式:

 orderproto.Order item = mOrders.get(position);
Binding binder = BindingFactory.GetBindig("order_list_view", v);
binder.Execute(v, item);

数据绑定描述

下面详细讲解实现方式,为了达到数据绑定功能首先要有一个信息描述;由于接触android不久所以暂不清楚如何给控件添加一些自定义的XML描述,所以直接采用了ContentDescription这个属性来完成绑定描述的工作。约定绑定表达式为"bind:member".

当有了绑定描述信息后要做的事情就是找出容器中有那些控件存在绑定描述和对应的绑定的属性。

 private void findChild(View view) {
ViewGroup bg = null;
View nextChild = null;
if (view instanceof ViewGroup)
bg = (ViewGroup) view;
if (bg != null) {
for (int i = ; i < bg.getChildCount(); ++i) {
nextChild = bg.getChildAt(i);
if (nextChild instanceof ViewGroup) {
findChild(nextChild);
} else { CharSequence cs = nextChild.getContentDescription();
String bindinfo = null;
if (cs != null)
bindinfo = nextChild.getContentDescription().toString();
if (bindinfo != null && bindinfo.indexOf("bind:") == ) {
String member = bindinfo.split(":")[];
mControls
.add(new Control(nextChild.getId(),
new ControlHandler(
nextChild.getClass(), member))); }
} }
}
}

实现代码并不复杂,递归的方式寻找控件如果存在绑定信息的情况下添加了绑定列表中。

数据绑定接口

由于数据输出控件是不固定的,因此需要制定一个绑定接口;具体控件绑定就通过实现该接口来处理具体的工作。

 public interface IControlDataBinder {
void SetValue(View e,Object value,String format);
Object GetValue(View e);
}

TextView的实现

 public class TextViewDataBinder implements IControlDataBinder {

     @Override
public void SetValue(View e, Object value, String format) {
// TODO Auto-generated method stub
TextView control=(TextView)e;
if(format==null || format.equals(""))
{
control.setText(value.toString());
}
else
{
control.setText(String.format(format, value));
}
} @Override
public Object GetValue(View e) {
// TODO Auto-generated method stub
TextView control=(TextView)e;
return control.getText().toString();
} }

EditText的实现

 public class EditTextDataBinder implements IControlDataBinder {

     @Override
public void SetValue(View e, Object value, String format) {
// TODO Auto-generated method stub
EditText control=(EditText)e;
if(format==null || format.equals(""))
{
control.setText(value.toString());
}
else
{
control.setText(String.format(format, value));
}
} @Override
public Object GetValue(View e) {
// TODO Auto-generated method stub
EditText control=(EditText)e;
return control.getText().toString();
} }

对于其它控件则根据自己需要来实现。

对象数据获取

在java似乎不存在象c#那样的属性,要么是Field或方法。所以通过名称来得到绑定信息就要做一些简单的处理,如果Field不存储则要检索一下对应的get方法。

 public MemberInvoke(Class<?> type,String name)
{
try
{
mField = type.getField(name);
mIsMethod= false;
mInvalid = false;
}
catch(Exception e)
{ }
if(mInvalid)
{
try
{
mGetMethod = type.getMethod("get"+name);
mIsMethod= true;
mInvalid = false;
}
catch(Exception e)
{ }
}
}

数据绑定的具体工作

通过名称找到对应的Binding对象,所以名称和View在使用的时候必须保持一致。

 private static HashMap<String, Binding> mBindingTbl = new HashMap<String, Binding>();
public static Binding GetBindig(String name,View view)
{
Binding result = null;
result = mBindingTbl.get(name);
if(result ==null)
{
result = new Binding(view);
mBindingTbl.put(name, result);
}
return result;
}

找到相应Binding对象后直接处理所有需要绑定的控件即可。

 public void Execute(View view, Object source) {
try {
for (Control item : mControls) {
item.Handler.ToControl(view.findViewById(item.ID), source);
}
} catch (Exception e) { }
}

下载

ikende.rar (4.00 kb)

Android下实现数据绑定功能的更多相关文章

  1. Delphi在Android下实现BroadcastReceiver功能(举例在Delphi下获取USB外设拔插消息)

    在Android里,用java通过实现BroadcastReceiver接口,就可以获得Intent消息.可是Delphi程序不能直接实现JBroadcastReceiver,如何能够实现类似Java ...

  2. Xamarin. Android实现下拉刷新功能

    PS:发现文章被其他网站或者博客抓取后发表为原创了,给图片加了个水印 下拉刷新功能在安卓和iOS中非常常见,一般实现这样的功能都是直接使用第三方的库,网上能找到很多这样的开源库.然而在Xamarin. ...

  3. [转]Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能

    版权声明:本文出自郭霖的博客,转载必须注明出处. 转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9255575 最近项目中需要用到L ...

  4. Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能 (转)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9255575 最 近项目中需要用到ListView下拉刷新的功能,一开始想图省事,在 ...

  5. android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-refresh)

    Android实现上拉加载更多功能以及下拉刷新功能, 采用了目前比较火的PullToRefresh,他是目前实现比较好的下拉刷新的类库. 目前他支持的控件有:ListView, ExpandableL ...

  6. android 下 利用webview实现浏览器功能

    android 下 利用webview实现浏览器功能(一): 1.界面添加WEBVIEW控件. 2.在界面.JAVA代码页面(protected void onCreate(Bundle savedI ...

  7. Android StaggeredGrid 加下拉刷新功能 PullToRefresh

    https://github.com/etsy/AndroidStaggeredGrid  用的github上面提供瀑布流,继承于abslistview,回收机制不错,并且提供了OnScrollLis ...

  8. Android 下的usb框架及功能点【转】

    本文转载自:https://blog.csdn.net/tianruxishui/article/details/37902959 有关USB android框架的链接 http://blog.sin ...

  9. Android 高仿微信(QQ)滑动弹出编辑、删除菜单效果,增加下拉刷新功能

    不可否认,微信.QQ列表的滑动删除.编辑功能着实很经典(从IOS那边模仿过来的),然.Android这边,对列表的操作,其实大多还停留上下文菜单来实现. Android如何实现list item的滑动 ...

随机推荐

  1. Codeforces 939E Maximize! (三分 || 尺取)

    <题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...

  2. leetcode刷题正则表达式

    题目链接:https://leetcode-cn.com/problems/regular-expression-matching/ 这道题用到了动态规划: 关于动态规划请参考这篇博文:https:/ ...

  3. 手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)

    通过: 手写数字识别  ----卷积神经网络模型官方案例详解(基于Tensorflow,Python) 手写数字识别  ----Softmax回归模型官方案例详解(基于Tensorflow,Pytho ...

  4. 大数据 - Java基础:读取键盘输入的方法

    Java中获取键盘输入值的三种方法 程序编写中,从键盘获取数据是一件非常普通又平常的事 C:scanf() C++:cin() C#:Read().ReadKey().ReadLine() Java没 ...

  5. SQL Server查询重复数据

    1.查询单列重复: select * from test where name in (select name from test group by name having count (name) ...

  6. selenium python 设置窗口打开大小

    1. 窗口最大化 1 driver.maximize_window() 2. 设置窗口大小 1 driver.set_window_size(1920,1080) #分辨率1920 x 1080

  7. 1、了解计算机与操作系统发展阶段 2、选择一个具体的操作系统,结合计算机与操作系统的发展阶段,详细了解其渊源、发展过程、趋势,整理成简洁美观的图文博客发布。 Windows Mac os x Unix Linux Android 等。

    1.了解计算机与操作系统发展阶段 操作系统并不是与计算机硬件一起诞生的,它是在人们使用计算机的过程中,为了满足两大需求:提高资源利用率.增强计算机系统性能,伴随着计算机技术本身及其应用的日益发展,而逐 ...

  8. 小程序block标签配合if和else 和 动态修改标题栏

    <block wx:if="{{aaaa}}"> <view>aaaa为 true,显示</view> </block> <b ...

  9. Senparc之OAuth原理

    今天学习了网易云课堂的 盛派的微信开发课程之OAuth微信网页授权:OAuth原理,边听边来波笔记: 1.什么是OAuth? OAuth 你的接口提供给别人使用,你需要提供Oauth,可以让被人使用, ...

  10. WPF:How to display a Bitmap on Image control

    一个Bitmap文件,叫做screenShotFile, 你可以这样显示到Image控件上. BitmapImage bi = new BitmapImage();            bi.Beg ...