最近的这个项目app,是一个采集类的app。有很多张表,每一张表需要填写很多的字段值。那么填完之后,在交换界面可以为数据做修改,插入操作。创建数据库框架使用的是greendao。事先需要为每一张表编写一个bean。那么现在问题来了,这个bean每一个属性,需要和交互界面每一个EditText相匹配。试想这里有20多张表,看这工作量有多大。为此,在开发这个app之初,我特意要求我的小伙伴在编写每一个EditeText的ID必须为控件类型_表名_字段名来唯一区分每一个ID,同时每一张表格的bean自然就是每一属性对应表格的字段。
接下来看一下数据的插入操作。数据的插入得需要从交互界面获取值,那么一开始我是使用这样的方式来获取值得。如下面的代码。这里只是截取了一部分代码,是不是要崩溃,重复写类似的代码。因为这里只是一张表,一张表的插入操作,还没有涉及到更新。而更新的话,则需要从数据库里面取出值,然后填到EditeText上面、这样又是和一一的和每一个EditText编写代码。这工作量巨大。
entityPersonInfo.setPersoninfo_name(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_name));
entityPersonInfo.setPersoninfo_sex(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_sex));
entityPersonInfo.setPersoninfo_rename(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_rename));
entityPersonInfo.setPersoninfo_picture(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_picture));
entityPersonInfo.setPersoninfo_nationlity(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_nationlity));
entityPersonInfo.setPersoninfo_catetype(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_catetype));
entityPersonInfo.setPersoninfo_catecode(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_catecode));
entityPersonInfo.setPersoninfo_catedate(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_catedate));
entityPersonInfo.setPersoninfo_birthdate(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_birthdate));
entityPersonInfo.setPersoninfo_heath(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_heath));
entityPersonInfo.setPersoninfo_belief(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_belief));
entityPersonInfo.setPersoninfo_marital(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_marital));
entityPersonInfo.setPersoninfo_admincode(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_admincode));
entityPersonInfo.setPersoninfo_birthadress(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_birthadress));
entityPersonInfo.setPersoninfo_height(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_height));
entityPersonInfo.setPersoninfo_nature(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_nature));
entityPersonInfo.setPersoninfo_bloodtype(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_bloodtype));
entityPersonInfo.setPersoninfo_age(EditUtils.getEdtValue(rootView,R.id.edt_personinfo_age));
无奈,只要寻找其他方法。那么之前规定的每一个EditText以及bean字段是存在关联的,我们这里可以通过反射方式来获取bean类所有的属性,再将属性转为字符,再拼接其他字符,这个就可以组成EditText的ID,那么这样就可以来创建Bean(表)和交互界面的EditText关联。我们来看一下代码。那么这里只需要将上下文传进来,接着就是我们的表格,以及所有这些EditeText的父View。说白了,就是动态获取ID。那么现在就可以运用到其他的表格上面去了。下面这个函数是使用Java的反射,为属性设置值。
/**
*
* @param context
* @param bean
* @param rootView
*/
public static void MatchSetEditTextToField(Context context, Object bean, View rootView){
try {
Class<?> cls=bean.getClass();
Field[] fields=cls.getDeclaredFields();
for (Field field:fields){
String type = field.getType().getSimpleName();
if (type.equals("String")){
String strBeanField=field.getName();
String edtID="edt_"+strBeanField;
int resId = context.getResources().getIdentifier(edtID, "id",context.getPackageName());
EditText edt=(EditText)rootView.findViewById(resId);
String value=edt.getText().toString();
field.set(bean,value);
}
}
}catch (Exception e){
Log.e("refect","反射的时候发生了错误");
}
}
我们刚才也说了,可以设置值的。这里使用的是读取EditText,然后将值填写到bean中,我们来看一下代码。
public static void MatchSetFieldToEditText(Context context, Object bean, View rootView){
Class<?> cls=bean.getClass();
try {
Field[] fields=cls.getDeclaredFields();
//personinfo_bloodtype
String packageName="";
for (Field field:fields ){
String type = field.getType().getSimpleName();
if (type.equals("String")){
String strBeanField=field.getName();
String edtID="edt_"+strBeanField;
int resId = context.getResources().getIdentifier(edtID, "id",context.getPackageName());
EditText edt=(EditText)rootView.findViewById(resId);
String value=String.valueOf(field.get(bean));
edt.setText(value);
}
}
}catch (Exception e){
e.fillInStackTrace();
}
}