Android下实现数据绑定功能

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

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

        传统方式:

 1 EditText editor = (EditText)v.findViewById(R.id.orders_orderid);
 2         editor.setText(item.getOrderID());
 3         editor =(EditText)v.findViewById(R.id.orders_employee);
 4         editor.setText(item.getEmployee());
 5         editor=(EditText)v.findViewById(R.id.orders_customer);
 6         editor.setText(item.getCustomer());
 7         editor =(EditText)v.findViewById(R.id.orders_orderdate);
 8         editor.setText(item.getOrderDate());
 9         editor =(EditText)v.findViewById(R.id.orders_requireddate);
10         editor.setText(item.getRequiredDate());
11         editor=(EditText)v.findViewById(R.id.orders_shipaddress);
12         editor.setText(item.getShipAddress());
13         editor =(EditText)v.findViewById(R.id.orders_shipcity);
14         editor.setText(item.getShipCity());
15         editor=(EditText)v.findViewById(R.id.orders_shipname);
16         editor.setText(item.getShipName());
17         editor =(EditText)v.findViewById(R.id.orders_shippedDate);
18         editor.setText(item.getShippedDate());
19         editor =(EditText)v.findViewById(R.id.orders_shipregion);
20         editor.setText(item.getShipRegion());

        数据绑定方式:

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

数据绑定描述

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

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

 1 private void findChild(View view) {
 2         ViewGroup bg = null;
 3         View nextChild = null;
 4         if (view instanceof ViewGroup)
 5             bg = (ViewGroup) view;
 6         if (bg != null) {
 7             for (int i = 0; i < bg.getChildCount(); ++i) {
 8                 nextChild = bg.getChildAt(i);
 9                 if (nextChild instanceof ViewGroup) {
10                     findChild(nextChild);
11                 } else {
12 
13                     CharSequence cs = nextChild.getContentDescription();
14                     String bindinfo = null;
15                     if (cs != null)
16                         bindinfo = nextChild.getContentDescription().toString();
17                     if (bindinfo != null && bindinfo.indexOf("bind:") == 0) {
18                         String member = bindinfo.split(":")[1];
19                         mControls
20                                 .add(new Control(nextChild.getId(),
21                                         new ControlHandler(
22                                                 nextChild.getClass(), member)));
23 
24                     }
25                 }
26 
27             }
28         }
29     }

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

数据绑定接口

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

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

        TextView的实现

 1 public class TextViewDataBinder implements IControlDataBinder {
 2 
 3     @Override
 4     public void SetValue(View e, Object value, String format) {
 5         // TODO Auto-generated method stub
 6         TextView control=(TextView)e;
 7         if(format==null || format.equals(""))
 8         {
 9             control.setText(value.toString());
10         }
11         else
12         {
13             control.setText(String.format(format, value));
14         }
15     }
16 
17     @Override
18     public Object GetValue(View e) {
19         // TODO Auto-generated method stub
20         TextView control=(TextView)e;
21         return control.getText().toString();
22     }
23 
24 }

        EditText的实现

 1 public class EditTextDataBinder implements IControlDataBinder {
 2 
 3     @Override
 4     public void SetValue(View e, Object value, String format) {
 5         // TODO Auto-generated method stub
 6         EditText control=(EditText)e;
 7         if(format==null || format.equals(""))
 8         {
 9             control.setText(value.toString());
10         }
11         else
12         {
13             control.setText(String.format(format, value));
14         }
15     }
16 
17     @Override
18     public Object GetValue(View e) {
19         // TODO Auto-generated method stub
20         EditText control=(EditText)e;
21         return control.getText().toString();
22     }
23 
24 }

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

 对象数据获取

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

 1 public MemberInvoke(Class<?> type,String name)
 2     {
 3         try
 4         {
 5             mField = type.getField(name);
 6             mIsMethod= false;
 7             mInvalid = false;
 8         }
 9         catch(Exception e)
10         {
11             
12         }
13         if(mInvalid)
14         {
15             try
16             {
17                 mGetMethod = type.getMethod("get"+name);
18                 mIsMethod= true;
19                 mInvalid = false;
20             }
21             catch(Exception e)
22             {
23                 
24             }
25         }
26     }

数据绑定的具体工作

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

 1 private static HashMap<String, Binding> mBindingTbl = new HashMap<String, Binding>();
 2     public static Binding GetBindig(String name,View view)
 3     {
 4         Binding result = null;
 5         result = mBindingTbl.get(name);
 6         if(result ==null)
 7         {
 8             result = new Binding(view);
 9             mBindingTbl.put(name, result);
10         }
11         return result;
12     }

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

1 public void Execute(View view, Object source) {
2         try {
3             for (Control item : mControls) {
4                 item.Handler.ToControl(view.findViewById(item.ID), source);
5             }
6         } catch (Exception e) {
7 
8         }
9     }

下载   

ikende.rar (4.00 kb)

原文地址:https://www.cnblogs.com/smark/p/3298270.html