2015年4月11日 星期六

AToATool


               /*
before
               editholder.getName().setText(positionobject.getName());
                editholder.getAddress().setText(positionobject.getAddress());
                editholder.getFixLineTel().setText(positionobject.getFixLineTel());
                editholder.getMobile().setText(positionobject.getMobile());
                */

after
      AToATool.instance.setAllTextViewByGetter(positionobject, context, editholder.getConvertView());


----------------------package suntone.lfo.com.suntonehelper.tool;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.googlecode.openbeans.IntrospectionException;
import com.googlecode.openbeans.Introspector;
import com.googlecode.openbeans.PropertyDescriptor;
import com.lfo.suntone.db.Supplier;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on 2015/4/11.
 */
public enum AToATool {
    instance;
    public List<Method> getGetters(Class claz){
        List<Method> getter=new ArrayList<Method>();
        try {
            for(PropertyDescriptor propertyDescriptor :
                    Introspector.getBeanInfo(claz).getPropertyDescriptors()){

                // propertyEditor.getReadMethod() exposes the getter
                // btw, this may be null if you have a write-only property
                Method m =propertyDescriptor.getReadMethod();
                if(m!=null){
                    getter.add(m) ;
                }

            }
            return getter;
        } catch (IntrospectionException e) {
            e.printStackTrace();
            return getter;
        }
    }
    public List<Method> getSetters(Class claz){
        List<Method> setters=new ArrayList<Method>();
        try {
            for(PropertyDescriptor propertyDescriptor :
                    Introspector.getBeanInfo(claz).getPropertyDescriptors()){

                // propertyEditor.getReadMethod() exposes the getter
                // btw, this may be null if you have a write-only property
                Method m =propertyDescriptor.getWriteMethod();
                if(m!=null){
                    setters.add(m) ;
                }

            }
            return setters;
        } catch (IntrospectionException e) {
            e.printStackTrace();
            return setters;
        }
    }
    public Method findMethod(String str,List<Method> list){
        if(str==null){
            Utils.instance.log("findMethod str==null");
            return null;
        }
        if(str.isEmpty()){
            Utils.instance.log("findMethod strisEmpty");
            return null;
        }
        for(Method m:list){
            if(m!=null){
                String mname=m.getName();
                mname=mname.substring(3);//去掉GET
                if(mname.toLowerCase().equals(str.toLowerCase())){
                    return m;
                }

            }

        }

        return null;

    }
    public <T> void setAllTextViewByGetter(T t, Context context, View layoutView) {

        Class claz = t.getClass();
        Field[] field = claz.getDeclaredFields();
        List<Method> getters =getGetters(claz);
//1.no getter method continue;
//2.no equals  name TextView continue;


        for (Field f : field) {
            String name = f.getName();
            Method m = findMethod(name, getters);
            if (m == null) {
                Utils.instance.log("setAllTextViewByGetter not find methhod  in getters of name : " + name);
                continue;
            }
            int resID = context.getResources().getIdentifier(name, "id", context.getPackageName());
            TextView textview = (TextView) layoutView.findViewById(resID);
            try {
                if (textview == null) {
                    Utils.instance.log("setAllTextViewByGetter not find TextView of " + name);
                    continue;
                }
                String str = "";
                str = String.valueOf(m.invoke(t));
                if(str.toLowerCase().equals("null")){
                    continue;
                }
                if (str != null) {
                    textview.setText(str);

                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
                return;
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                return;
            }
        }
    }


    public <T> void settingByAllTextView(T t, Context context, View layoutView) {

        //1.if setter param type is not string or integer then skip
        //2.setter only have one parameter
        Class claz=t.getClass();
        Field[] field = claz.getDeclaredFields();
        List<Method> setters =getSetters(claz);
        for (Field f : field) {
            String name = f.getName();
            Method m = findMethod(name, setters);
            if (m == null) {
                Utils.instance.log("settingByAllTextView not find methhod  in setters of name : " + name);
                continue;
            }
            int resID = context.getResources().getIdentifier(name, "id", context.getPackageName());
            TextView textview = (TextView) layoutView.findViewById(resID);
            try {
                if (textview == null) {
                    Utils.instance.log("settingByAllTextView not find TextView of " + name);
                    continue;
                }
                Class[] parameterTypes = m.getParameterTypes();
                if(parameterTypes.length>1){
                    Utils.instance.log("settingByAllTextView parameterTypes.length>1");
                    continue;
                }
                Class firstparamclass=parameterTypes[0];
                String str = textview.getText().toString();
                if(str.isEmpty()){
                    continue;
                }
                if(firstparamclass.getSimpleName().equals("String")){
                    m.invoke(t,str);
                    continue;
                }
                if(firstparamclass.getSimpleName().equals("Integer")){
                    m.invoke(t,Integer.valueOf(str));
                    continue;
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
                return;
            } catch (InvocationTargetException e) {
                e.printStackTrace();
                return;
            }
        }

    }
}

沒有留言:

張貼留言