我想使用 for 循环将值传递给 add-temple 类。
我的添加寺庙功能:
// Adding new temple
public void Add_Temple(kovil Kovil) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TMPNAME, Kovil.gettemplename());
values.put(KEY_TMPTYPE, Kovil.gettempletype());
values.put(KEY_LATITUDE, Kovil.getlatitude());
values.put(KEY_LONGITUDE, Kovil.getlongitude());
values.put(KEY_IMGNAME, Kovil.getimage_name());
values.put(KEY_YEARBUILD, Kovil.getyear_build());
values.put(KEY_ADDRESS, Kovil.getaddress());
values.put(KEY_CITY, Kovil.getcity());
values.put(KEY_EMAIL, Kovil.getemail());
values.put(KEY_WEB, Kovil.getwebsite());
values.put(KEY_TEL1, Kovil.gettelephone1());
values.put(KEY_TEL2, Kovil.gettelephone2());
values.put(KEY_DESCRI, Kovil.getDescription());
// Inserting Row
db.insert(TABLE_TEMPLE, null, values);
db.close(); // Closing database connection
}
我的 for 循环将值发送到 Add Temple 函数:
kovil insertData1 = new kovil("temple_name", "temple_type", "latitude", "longitude", "image_name", "year_build", "address", "city", "email", "website", "telephone1", "telephone2", "Description");
// dbhand .Add_Temple(insertData1 );
kovil insertData2 = new kovil("temple_name2", "temple_type2", "latitude2", "longitude2", "image_name2", "year_build2", "address2", "city2", "email2", "website2", "telephone12", "telephone22", "Description2");
// dbhand .Add_Temple(insertData2 );
for(int i=0;i<2; i++){
dbhand .Add_Temple("insertData"+i);
}
我在 for 循环中遇到语法错误。有人可以帮我写这个 for 循环吗?
错误消息
Multiple markers at this line
- insertData cannot be resolved to a variable
- The method Add_Temple(kovil) in the type Dbhandler is not applicable for the arguments
(String)
最佳答案
您的 Add_Temple 函数参数的类型为“kovil”,您正在传递 String 类型。
例如,您应该将“kovil”对象放入 ArrayList,然后像这样迭代它:
ArrayList<kovil> arrayList = new ArrayList<>();
arrayList.add(insertData1);
arrayList.add(insertData2);
for (int i=0; i<arrayList.size(); i++){
dbhand.Add_Temple(arrayList.get(i));
};
关于java - 使用 for 循环传递对象。安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21696461/