首页 技术 正文
技术 2022年11月14日
0 收藏 697 点赞 2,635 浏览 3900 个字

在编写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)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,493
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297