Note: Depending on the actual situation, the type of the field in the entity is added, and the control type is judged before it can be used. The controls here are only TextView EditText
Entity class fields have only string int type, control with value added tag, value is consistent with field value of entity class
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 voidGetvauletomode(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 voidGetVauleToMode2(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 voidSetvauletoview(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 ());
}elseif (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 ();
}
}
}
}