Android uses reflection mechanism to assign values to entity class attributes

Source: Internet
Author: User

In the Android project, sometimes encountered from the network to get JSON type data, assigned to the entity class, the entity class attributes can be one of the assignment, if the entity class has many properties, the assignment may be a long time, fortunately, Java provides us with a reflection mechanism. Here's how I can use the Java reflection mechanism to assign values to an entity class in my Android.

Using the reflection mechanism to assign values to entity class properties in Android, you first need to import two packages


Import Java.lang.reflect.Field;

Import Java.lang.reflect.Method;

Assigning a value to an entity class first requires an entity class, in which I simply define a student entity class Studentinfobean:

 Public classStudentinfobean {Private intID; PrivateString Stuname; PrivateString Stugender; PrivateString Stuidcard; PrivateString Stuclass; PrivateString address;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString Getstuname () {returnStuname; }     Public voidsetstuname (String stuname) { This. Stuname =Stuname; }     PublicString Getstugender () {returnStugender; }     Public voidSetstugender (String stugender) { This. Stugender =Stugender; }     PublicString Getstuidcard () {returnStuidcard; }     Public voidSetstuidcard (String stuidcard) { This. Stuidcard =Stuidcard; }     PublicString Getstuclass () {returnStuclass; }     Public voidSetstuclass (String stuclass) { This. Stuclass =Stuclass; }     PublicString getaddress () {returnaddress; }     Public voidsetaddress (String address) { This. Address =address; }    }

The following defines a Utils class that defines the method in the Utils class, see the code:

 Public classUtils {Private Static FinalString TAG = "Utils";  PublicObject Setobjectvalue (Object object, String JSON)throwsException {if(Object = =NULL)            return NULL; LOG.D (TAG,"Setobjectvalue"); Class<?> Clazz =Object.getclass (); Field[] Fields=Clazz.getdeclaredfields ();  for(Field field:fields) {log.d (TAG,"Field type:" + field.getname ());//type of print field            if(Field.getgenerictype (). toString (). Equals ("Class java.lang.String")) {Method Method= Object.getclass (). GetMethod ("set" + Getmethodname (Field.getname ()), String.class);            Method.invoke (object, Parsejson (JSON, Field.getname ())); }            if(Field.getgenerictype (). toString (). Equals ("Class Java.lang.Integer"))//{Method Method= Object.getclass (). GetMethod ("set" + Getmethodname (Field.getname ()), Integer.class);            Method.invoke (object, Parsejson (JSON, Field.getname ())); }                    }        returnobject; }    /*** Parse JSON **/    Privatestring Parsejson (String json_string, String name) {string str= "No Info";        Jsonobject JSON; Try{JSON=NewJsonobject (json_string); JSON= Json.getjsonobject ("Studentinfo"); STR=json.getstring (name); } Catch(jsonexception e) {e.printstacktrace (); }        returnstr; }    /*** Capitalize the first letter of the property name **/     Publicstring Getmethodname (String fieldName) {byte[] bytes =fieldname.getbytes (); bytes[0] = (byte) (Bytes[0]-' a ' + ' a '); return NewString (bytes); }}

In the above code, a Setobjectvalue method is defined, the method receives two parameters, the first is a parameter of type object, in this case a Studentinfobean object is received, the second parameter is a string type of JSON data, is populated into the Studentinfobean object.

Android uses reflection mechanism to assign values to entity class attributes

Related Article

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.