【Android開發】&利用Java反射獲取、設置屬性值來關聯EditText數據

最近的這個項目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();
       }

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章