First of all, Activeandroid relies on annotations to work.
@Table (name = "UserBean") Public classUserBeanextendsModel {@Column (name= "UID") PublicString uid; @Column (Name= "Nick_name") PublicString Nick_name; PublicString Getuid () {returnuid; } Public voidSetUid (String uid) { This. UID =uid; } PublicString Getnick_name () {returnNick_name; } Public voidsetnick_name (String nick_name) { This. Nick_name =Nick_name; }}
Add a Table annotation to the class and add a Column annotation to the members of the class, so we can look at the definitions of the two annotations first.
@Target (Elementtype.type) @Retention (retentionpolicy.runtime) public @Interface Table { publicstaticfinal String Default_id_name = "ID"; Public String name (); Public default Default_id_name;}
table is defined as above, there are two members, respectively ID and name, usually we just need to set name,ID name is set by default. This name is the table name of the database, and theID is the name of the ID field in the table.
The definition of Column is complex, but we can imagine that there must be a name member, andname is the field name in the table.
Activeandroid then uses a Tableinfo class to save the connection information for the class and table. Let's take a look at this class.
Packagecom.activeandroid;/** Copyright (C) Michael Pardo * * Licensed under the Apache License, Version 2.0 (the "License"); Use this file except in compliance with the License. * Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ImportJava.lang.reflect.Field;Importjava.util.Collection;Importjava.util.Collections;ImportJava.util.LinkedHashMap;Importjava.util.LinkedList;Importjava.util.List;ImportJava.util.Map;Importandroid.text.TextUtils;ImportAndroid.util.Log;ImportCom.activeandroid.annotation.Column;Importcom.activeandroid.annotation.Table;Importcom.activeandroid.util.ReflectionUtils; Public Final classTableinfo {////////////////////////////////////////////////////////////////////////////////////// //PRIVATE Members ////////////////////////////////////////////////////////////////////////////////////// Privateclass<?extendsModel>Mtype; PrivateString Mtablename; PrivateString Midname =Table.default_id_name; PrivateMap<field, string> mcolumnnames =NewLinkedhashmap<field, string>(); ////////////////////////////////////////////////////////////////////////////////////// //Constructors ////////////////////////////////////////////////////////////////////////////////////// PublicTableinfo (class<?extendsModel>type) {Mtype=type; FinalTable tableannotation = type.getannotation (table.class); if(Tableannotation! =NULL) {Mtablename=Tableannotation.name (); Midname=tableannotation.id (); } Else{mtablename=Type.getsimplename (); } //manually add the ID column since it is not declared like the other columns.Field IDfield =Getidfield (type); Mcolumnnames.put (IDfield, midname); List<Field> fields =NewLinkedlist<field>(Reflectionutils.getdeclaredcolumnfields (type)); Collections.reverse (fields); for(Field field:fields) {if(Field.isannotationpresent (Column).class)) { FinalColumn columnannotation = field.getannotation (column.class); String ColumnName=Columnannotation.name (); if(Textutils.isempty (columnName)) {ColumnName=Field.getname (); } mcolumnnames.put (field, columnName); } } } ////////////////////////////////////////////////////////////////////////////////////// //Public METHODS ////////////////////////////////////////////////////////////////////////////////////// Publicclass<?extendsModel>GetType () {returnMtype; } PublicString Gettablename () {returnMtablename; } PublicString Getidname () {returnMidname; } PublicCollection<field>GetFields () {returnMcolumnnames.keyset (); } PublicString getcolumnname (Field field) {returnmcolumnnames.get (field); } PrivateField Getidfield (class<?>type) { if(Type.equals (Model.class)) { Try { returnType.getdeclaredfield ("MId"); } Catch(nosuchfieldexception e) {LOG.E ("Impossible!", e.tostring ()); } } Else if(Type.getsuperclass ()! =NULL) { returnGetidfield (Type.getsuperclass ()); } return NULL; }}
The first is the member variable
Private extends Model> mtype; Private String mtablename; Private String midname = table.default_id_name; Private New Linkedhashmap<field, string> ();
Mtype: The type of class that needs to be stored;
Mtablename: The table name of the stored class;
Midname: The ID field name of the table for the stored class;
Mcolumnnames: The mapping of the field names in the stored tables to the members in the class;
public tableinfo (class<? extends Model> Type" {mtype = type; final Table tableannotation = type.getannotation (Table.class ); if (tableannotation! = null = Tableannotation.name (); Midname = Tableannotation.id (); else {mtablename = Type.getsimplename (); }
...
}
Tableannotation saves the annotations of the stored class and reads the table and table ID names from the annotations, and if there are no annotations, the default class name is the table name.
Public extends model> type) { = type; ...... // manually add the ID column since it is not declared like the other columns. Field IDfield = Getidfield (type); Mcolumnnames.put (IDfield, midname);
......
}
Manually add the ID name of the table to the class ID relationship. Look at the Getidfield method:
PrivateField Getidfield (class<?>type) { if(Type.equals (Model.class)) { Try { returnType.getdeclaredfield ("MId"); } Catch(nosuchfieldexception e) {LOG.E ("Impossible!", e.tostring ()); } } Else if(Type.getsuperclass ()! =NULL) { returnGetidfield (Type.getsuperclass ()); } return NULL; }
A recursive, looking for the parent class through Getsuperclass (), until the parent class is model, where the mid member is returned.
PublicTableinfo (class<?extendsModel>type) { ...... List<Field> fields =NewLinkedlist<field>(Reflectionutils.getdeclaredcolumnfields (type)); Collections.reverse (fields); for(Field field:fields) {if(Field.isannotationpresent (Column).class)) { FinalColumn columnannotation = field.getannotation (column.class); String ColumnName=Columnannotation.name (); if(Textutils.isempty (columnName)) {ColumnName=Field.getname (); } mcolumnnames.put (field, columnName); } } }
As in this code, the members of all the labels that are to be stored in the class are removed by reflection, and their annotation field names are obtained, and the member names are substituted if the field names are not being unpacked. Finally, all of them are mapped into mcolumnnames with the member itself one by one.
The above is a tableinfo initialization process. The next time you read the code that takes all the labeled Class members out.
done!
Read Activeandroid source code (a)