用for循环传递一个对象。 Android的

时间:2014-02-11 08:15:52

标签: java android

我想使用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循环中遇到语法错误。有人可以帮我写这个循环请。

错误消息

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)

2 个答案:

答案 0 :(得分:3)

您的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));
};

答案 1 :(得分:1)

你应该做这样的事情

Kovil insertData1 = new Kovil("temple_name", "temple_type", "latitude",  "longitude", "image_name", "year_build", "address", "city", "email", "website", "telephone1", "telephone2",  "Description");


Kovil insertData2 = new Kovil("temple_name2", "temple_type2", "latitude2",  "longitude2", "image_name2", "year_build2", "address2", "city2", "email2", "website2", "telephone12", "telephone22",  "Description2");

Kovil[] array = new Kovil[]{insertData1, insertData2};

for(Kovil k : array) {
   dbhand .addTemple(k);
}

旁注:

请修复关键字,功能命名,遵循Java命名约定