码迷,mamicode.com
首页 > 移动开发 > 详细

java android布局里的控件值 反射绑定给实体类,实体类绑定给控件,表单提交绑定很有用

时间:2014-05-18 20:23:14      阅读:472      评论:0      收藏:0      [点我收藏+]

标签:android   style   class   c   java   tar   

 

注意了:根据实际情况,添加实体里字段的类型,控件类型的判断才可使用。这里控件只有TextView EditText

实体类字段只有String int类型,带值的控件添加tag ,值和实体类的字段值一致

 

 

 

package ice.ui.service;

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

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

public class ViewModeTool {



/**
* get all views
*
* @param parentView
* @return list
*/
public static synchronized List<View> getAllViews(View parentView) {
List<View> ices = new ArrayList<View>();

if (parentView instanceof ViewGroup) {
ViewGroup vp = (ViewGroup) parentView;

for (int i = 0; i < vp.getChildCount(); i++) {
View viewchild = vp.getChildAt(i);

if (viewchild instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) viewchild;
ices.addAll(getAllViews(vg));
}else{
if(viewchild.getTag()!=null&&!viewchild.getTag().toString().equals(""))
ices.add(viewchild);
}
}
}

return ices;
}

public static void GetVauleToMode(View parentView,Object moden )
{
List<View> listv = getAllViews(parentView);

Class classtype=moden.getClass();

for(int i=0;i<listv.size();i++)
{
Object Name=listv.get(i).getTag();
String Values="";
Field field = null;
try {

if (listv.get(i) instanceof EditText) {
EditText vg = (EditText) listv.get(i);
Values=vg.getText().toString();
}else if (listv.get(i)instanceof TextView) {
TextView vg = (TextView) listv.get(i);

Values=(String) vg.getText();

}

field = classtype.getDeclaredField(Name.toString());
field.setAccessible(true);
Type type = field.getType();
if(type.equals(int.class))
{
int iv = Integer.parseInt(Values);

field.setInt(moden, iv);

}else if(type.equals(String.class))
{
field.set(moden, Values);

}


} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}




}

public static void GetVauleToMode2(View parentView,Object moden )
{
List<View> listv = getAllViews(parentView);

Class classtype=moden.getClass();

for(int i=0;i<listv.size();i++)
{
Object Name=listv.get(i).getTag();
String Values="";

try {

Method method=classtype.getMethod("set"+Name, new Class[] {String.class });



if (listv.get(i) instanceof TextView) {
TextView vg = (TextView) listv.get(i);

Values=(String) vg.getText();

}else if (listv.get(i) instanceof EditText) {
EditText vg = (EditText) listv.get(i);
Values=vg.getText().toString();
}

method.invoke(moden, new Object[]{Values});

} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public static void SetVauleToView(View parentView,Object moden )
{
List<View> listv = getAllViews(parentView);

Class classtype=moden.getClass();

for(int i=0;i<listv.size();i++)
{
Object Name=listv.get(i).getTag();
Object Values="";
Field field = null;
try {
field = classtype.getDeclaredField(Name.toString());
field.setAccessible(true);
Values= field.get(moden);
if(Values!=null)
{

if (listv.get(i) instanceof EditText) {
EditText vg = (EditText) listv.get(i);
vg.setText(Values.toString());
}else if (listv.get(i) instanceof TextView) {
TextView vg = (TextView) listv.get(i);

vg.setText(Values.toString());

}

}

} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



}
}

java android布局里的控件值 反射绑定给实体类,实体类绑定给控件,表单提交绑定很有用,布布扣,bubuko.com

java android布局里的控件值 反射绑定给实体类,实体类绑定给控件,表单提交绑定很有用

标签:android   style   class   c   java   tar   

原文地址:http://www.cnblogs.com/lucoo/p/3732386.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!