2014-12-25 118 views
1

我在数据库项目中有6条记录。 For循环读取每条记录,然后检查转换为动态数组的结果。 但是 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

+0

帖子总计方法.. –

+0

循环前检查您的计数大小。 – Razgriz

+0

请再检查一次......我把照片放在烤面包上 – Developer

回答

0

循环是好的。在循环开始之前插入一条控制您的count变量的行。

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

如果是低于您的预期,请检查您的分贝的错误。

+0

请再次检查...我把照片放在吐司代码 – Developer

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(); 
} 
+0

没有我的朋友... 我想获取所有记录并记录循环内要完成的所有命令。 但是上面的代码中,只有三个是获取记录 – Developer

+0

好的,你的代码很好,也许你的数据库只有3个记录的值。 – Fihox

+0

:(数据库6条记录,在第一篇文章中显示图片... – Developer

0

我期待您的问题可能出在db.count_fielddb.get_latdb.get_log方法。

所以你可以把它们添加到你的问题。