for()循环不会持久

时间:2014-12-25 11:59:25

标签: android arrays android-sqlite android-looper

我在数据库项目中有6条记录。 for循环读取每条记录,然后检查转换到动态数组的结果。 但 for循环不能持续到最后! For循环检查第三条记录!

    int count = db.count_field("location", "id");

    geoAttrs = new ArrayList<StructureGeo>();
    StructureGeo geo = new StructureGeo();

    for (int i = 0; i < count; i++) {
        geo.lat = db.get_lat("location", i);
        geo.lang = db.get_log("location", i);
        geo.result = gps2m(latitude, longitude, geo.lat, geo.lang);
        geoAttrs.add(geo);
        Log.i("LOG", "Array Index #" + i + " = " + geoAttrs.get(i));
    }
    db.close();
    }

编辑

之后的吐司代码:

int count = db.count_field("location", "id");

和结果:

enter image description here

3 个答案:

答案 0 :(得分:0)

循环没问题。在循环开始之前插入一条控制count变量的行。

int count = db.count_field("location", "id");
    Log.d("LOG", "count equals " + count);
geoAttrs = new ArrayList<StructureGeo>();
StructureGeo geo = new StructureGeo();

如果它低于您的预期,那么请检查您的数据库是否存在错误。

答案 1 :(得分:0)

根据你的问题,你希望循环检查第3项而不是完整6,这可以通过使用这样的int变量来完成:

int count = db.count_field("location", "id");

geoAttrs = new ArrayList<StructureGeo>();
StructureGeo geo = new StructureGeo();
int counter = 0;
for (int i = 0; i < count; i++) {
      counter++;
if(counter == 3){
   geo.lat = db.get_lat("location", i);
    geo.lang = db.get_log("location", i);
    geo.result = gps2m(latitude, longitude, geo.lat, geo.lang);
    geoAttrs.add(geo);
    Log.i("LOG", "Array Index #" + i + " = " + geoAttrs.get(i));
    break; // ends loop
   }


}
db.close();
}

答案 2 :(得分:0)

我预计您的问题可能在于db.count_fielddb.get_latdb.get_log方法。

那么请你把它们添加到你的问题中。

相关问题