Android 绑定数据到界面控件

本文介绍了一种在Android中实现数据绑定的方法,通过反射机制将数据源中的属性值映射到TextView和EditText控件上,提高了开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Google出了个绑定数据到XML上的功能,不过目前实现有点麻烦。详情可以去android官网查看,爬不过墙的可以BAIDU一下 Android数据绑定 Data Binding等类似词语。

其实还有一种耗资源的实现方式,大家到可以试一下效果, 代码:

import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by anykaa on 15/10/29.
 */
public class BindingData {

    /**
     * 绑定对应属性值到TextView和EditText控件上
     * 规则:
     * TextView控件, myProperty-->txt_myProperty
     * EditText控件, myProperty-->etxt_myProperty
     *
     * @param root
     * @param bean
     */
    public static void bind(View root, Object bean) {
        if (root == null || bean == null) {
            return;
        }
        if (root instanceof ViewGroup) {
            View child;
            String tmp = null;
            Object arg = null;
            for (int i = 0; i < ((ViewGroup) root).getChildCount(); i++) {
                child = ((ViewGroup) root).getChildAt(i);
                if (child instanceof TextView) {
                    try {
                        tmp = child.getResources().getResourceName(child.getId());
                    } catch (Exception ex) {
                        LogEnrising.i("发现没有设置id属性的TextView控件");
                        continue;
                    }
                    tmp = tmp.substring(tmp.indexOf("/") + 1);

                    Field[] fields = bean.getClass().getDeclaredFields();
                    for (Field field : fields) {

                        if (tmp.equals("txt_" + field.getName())) {
                            field.setAccessible(true);
                            try {
                                if (field.getGenericType().toString().equals(
                                        "class java.lang.String") ||
                                        field.getGenericType().toString().equals(
                                                "class java.lang.Double") ||
                                        field.getGenericType().toString().equals(
                                                "class java.lang.Integer") ||
                                        field.getGenericType().toString().equals(
                                                "class java.lang.Short")) {
                                    Method m = (Method) bean.getClass().getMethod(
                                            "get" + getMethodName(field.getName()));

                                    arg = m.invoke(bean);// 调用getter方法获取属性值
                                }
                            } catch (Exception ee) {
                                LogEnrising.i("反射取值出错:" + field.getName());
                            }

                            ((TextView) child).setText(String.valueOf(arg));
                            break;
                        }
                    }

                } else if (child instanceof EditText) {
                    try {
                        tmp = child.getResources().getResourceName(child.getId());
                    } catch (Exception ex) {
                        LogEnrising.i("发现没有设置id属性的EditText控件");
                        continue;
                    }
                    tmp = tmp.substring(tmp.indexOf("/") + 1);

                    Field[] fields = bean.getClass().getDeclaredFields();
                    for (Field field : fields) {

                        if (tmp.equals("etxt_" + field.getName())) {
                            field.setAccessible(true);
                            try {
                                if (field.getGenericType().toString().equals(
                                        "class java.lang.String") ||
                                        field.getGenericType().toString().equals(
                                                "class java.lang.Double") ||
                                        field.getGenericType().toString().equals(
                                                "class java.lang.Integer") ||
                                        field.getGenericType().toString().equals(
                                                "class java.lang.Short")) {
                                    Method m = (Method) bean.getClass().getMethod(
                                            "get" + getMethodName(field.getName()));

                                    arg = m.invoke(bean);// 调用getter方法获取属性值
                                }
                            } catch (Exception ee) {
                                LogEnrising.i("反射取值出错:" + field.getName());
                            }

                            ((EditText) child).setText(String.valueOf(arg));
                            break;
                        }
                    }
                } else if (child instanceof ViewGroup) {
                    bind(child, bean);
                }
            }
        } else {
        }
    }

    // 把一个字符串的第一个字母大写、效率是最高的、
    private static String getMethodName(String fildeName) throws Exception {
        byte[] items = fildeName.getBytes();
        items[0] = (byte) ((char) items[0] - 'a' + 'A');
        return new String(items);
    }
}

这个方法是我参照别人写的东西做了相应改动,适合自己使用。

里面的控件命名规则我有说明,至于方法里面的实现方式我不再多说。

这个方法的弊端就是要扫描整个root(自己指定的View)的控件,但是使用很方便。

另外还有一点药注意,对于实体类的写法要注意。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值