以下代码适用于DAO和dBhelper类。我有3个表,每个表大约有6列,但我厌倦了“播放”每个方法的字段名称(插入,删除等等),并希望用替换下面的代码for-loop ,浏览字段名称和要插入dB的数据。
所以问题是:
如何将MySQLiteHelper.COLUMN_ID
这样的表达式替换为MySQLiteHelper.columnName
,以便将此oneliner嵌入到for-loop
中。
private String[] allColumns = {
MySQLiteHelper.COLUMN_level_1,
MySQLiteHelper.COLUMN_level_2,
MySQLiteHelper.COLUMN_level_3
};
String[] mydata = myobject.getData();
// Here I would like to use a for-loop for the following lines
values.put(MySQLiteHelper.COLUMN_level_1, mydata[0]);
values.put(MySQLiteHelper.COLUMN_level_2, mydata[1]);
values.put(MySQLiteHelper.COLUMN_level_3, mydata[2]);
答案 0 :(得分:1)
private String[] allColumns = {
MySQLiteHelper.COLUMN_level_1,
MySQLiteHelper.COLUMN_level_2,
MySQLiteHelper.COLUMN_level_3
};
String[] mydata = myobject.getData();
for(int i = 0; i < allColums.length(); i++){
values.put(allColums[i], mydata[i]);
}