Android for()循环不会持续
我在数据库项目中有6条记录。 For循环,读取每个记录,然后检查转换到动态数组的结果。 但是 For循环直到结束才能继续!让循环检查第三条记录Android for()循环不会持续,android,arrays,android-sqlite,android-looper,Android,Arrays,Android Sqlite,Android Looper,我在数据库项目中有6条记录。 For循环,读取每个记录,然后检查转换到动态数组的结果。 但是 For循环直到结束才能继续!让循环检查第三条记录 int count = db.count_field("location", "id"); geoAttrs = new ArrayList<StructureGeo>(); StructureGeo geo = new StructureGeo(); for (int i = 0; i < coun
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");
Log.d("LOG", "count equals " + count);
geoAttrs = new ArrayList<StructureGeo>();
StructureGeo geo = new StructureGeo();
如果低于预期值,则检查数据库中是否存在bug。根据您的问题,您希望循环检查第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();
}
我预计您的问题可能在于db.count_字段、db.get_lat或db.get_log方法
因此,请将它们添加到您的问题中。在循环之前检查您的计数大小。请再次检查。。。我将照片放在toast Code上logcat对您在for Loop中添加的日志有何评论?您在数据库中的任何字段是否为空?这可能会产生一个早期退出循环的错误No myfriend。。。我想获取所有记录,并记录循环中要执行的所有命令。但是上面的代码,只有三条正在获取记录OK,您的代码很好,可能您的数据库只有3条带值的记录。
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();
}