Using the reflection mechanism of Java, we can get a list of the methods in the object, the parameter list and the corresponding values, then we can use these to do a universal method of inserting the database.
The table names in the following methods are the same as the database names.
protected void Insert (Object ob) {
Contentvalues initialvalues = new Contentvalues ();
Class C = Ob.getclass ();
String className = C.getname ();
String tableName = classname.substring (Classname.tostring (). LastIndexOf (".") +1);
String devicename = "";
System.out.println ("TableName:" +tablename);
Field[] fields= c.getdeclaredfields ();
try {
for (Field f:fields) {
F.setaccessible (TRUE);
String str =f.tostring (). substring (f.tostring (). LastIndexOf (".") +1);
Object obj = f.get (ob);
if (Str.equals ("DeviceName"));
if (obj!=null&&!obj.tostring (). Equals ("0")) {
Initialvalues.put (str, obj+ "");
}
// System.out.println ("params:" +str+ "Value:" +f.get (OB). toString ());
}
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (Illegalaccessexception e) {
E.printstacktrace ();
}
Long ss = Db.insert (TableName, NULL, initialvalues);
}
Add or delete the check should be able to use the emission principle to do, here only added processing.
A universal way to add objects to the Sqlit database using the Java Reflection principle in Android