此处显示的是一种用于将值插入到 SQLite 数据库的列中的方法。 我以前从未使用过包含这么多列的数据库。该数据库中有超过 15 个表。数据库不是我设计的,是别人设计的。
如果可能,我该如何重构这个 android 方法以使其更好或更简洁,看起来我不能使用像 ArrayList 这样的集合对象,因为所有参数都不是一种类型,有多种类型,如 String, float 和整数。
因此这需要创建一个自定义 java 函数,但是这看起来并不值得付出努力。并且有 15 个不同的表需要 15 个自定义对象。
一些常识表明方法中的参数过多超过 4 或 5 个。不确定为什么这是普遍接受的思维方式。如果那是真的,那我的 java 方法真的需要理发了。或者更糟的是灌肠。
有什么想法吗?
public void insertNewRowInspectionPlan(int testOneInput, String testTwoInput,
int testThreeInput, float testFourInput, int TestFiveInput, int testSixInput,
int testSevenInput, int testEightInput, int TestNineInput, float testTenInput,
int testElevenInput, String testTwelveInput){
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_1, testOneInput);
contentValues.put(COLUMN_2, testTwoInput);
contentValues.put(COLUMN_3, testTheeInput);
contentValues.put(COLUMN_4, testFourInput);
contentValues.put(COLUMN_5, testFiveInput);
contentValues.put(COLUMN_6, testSixInput);
contentValues.put(COLUMN_7, testSevenInput);
contentValues.put(COLUMN_8, testEightInput);
contentValues.put(COLUMN_9, testNineInput);
contentValues.put(COLUMN_10, testTenInput);
contentValues.put(COLUMN_11, testElevenInput);
contentValues.put(COLUMN_12, testTwelveInput);
sqLiteDatabase.insert(INSPECTION_PLAN_TRANSACTION, null, contentValues);
}
最佳答案
使用 get set 方法创建一个用于测试的类,然后您不必在函数中传递那么多参数,您可以直接传递该类的引用变量
Example:
public class PropertiesContacts {
int _id;
String _title;
String _description ;
public int getID(){
return this._id;
}
public void setID(int id){
this._id = id;
}
public String gettitle(){
return this._title;
}
public void settitle(String title){
this._title = title;
}
public String getdescription (){
return this._description ;
}
public void setdescription (String description ){
this._description = description ;
}
}
Set properties value in activity
Like
PropertiesContacts obj=new PropertiesContacts ();
obj._id=1;
obj._title ="amit";
obj._description ="test";
insertNewRowInspectionPlan(obj);
Hope you will understan
关于java - SQLite插入函数/方法参数参数太多,如何重构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17717910/