The control value reflected in the Java Android layout is bound to the entity class, the entity class is bound to the control, and the form submission binding is useful

Source: Internet
Author: User

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 ();
}
}



}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.