我正在使用while循环遍历游标,然后输出数据库中每个点的经度和纬度值。
由于某种原因,它不会返回光标中的经度和纬度值的最后一个(或者首先取决于我是否使用Cursor.MoveToLast)。
这是我的代码:
public void loadTrack() {
SQLiteDatabase db1 = waypoints.getWritableDatabase();
Cursor trackCursor = db1.query(TABLE_NAME, FROM, "trackidfk=1", null, null, null,ORDER_BY);
trackCursor.moveToFirst();
while (trackCursor.moveToNext()) {
Double lat = trackCursor.getDouble(2);
Double lon = trackCursor.getDouble(1);
//overlay.addGeoPoint( new GeoPoint( (int)(lat*1E6), (int)(lon*1E6)));
System.out.println(lon);
System.out.println(lat);
}
}
从此我得到:
04-02 15:39:07.416:INFO / System.out(10551):3.0 04-02 15:39:07.416:INFO / System.out(10551):5.0 04-02 15:39:07.416:INFO / System.out(10551):4.0 04-02 15:39:07.416:INFO / System.out(10551):5.0 04-02 15:39:07.416:INFO / System.out(10551):5.0 04-02 15:39:07.416:INFO / System.out(10551):5.0 04-02 15:39:07.416:INFO / System.out(10551):4.0 04-02 15:39:07.416:INFO / System.out(10551):4.0 04-02 15:39:07.416:INFO / System.out(10551):3.0 04-02 15:39:07.416:INFO / System.out(10551):3.0 04-02 15:39:07.416:INFO / System.out(10551):2.0 04-02 15:39:07.416:INFO / System.out(10551):2.0 04-02 15:39:07.493:INFO / System.out(10551):1.0 04-02 15:39:07.493:INFO / System.out(10551):1.0
7组值,我应该得到8组。
感谢。
答案 0 :(得分:22)
moveToNext()有两个功能。它返回一个布尔值,表示 是下一个,但同时它继续并移动光标。
public void loadTrack() {
SQLiteDatabase db1 = waypoints.getWritableDatabase();
Cursor trackCursor = db1.query(TABLE_NAME, FROM, "trackidfk=1", null, null, null,ORDER_BY);
trackCursor.moveToFirst();
do {
Double lat = trackCursor.getDouble(2);
Double lon = trackCursor.getDouble(1);
//overlay.addGeoPoint( new GeoPoint( (int)(lat*1E6), (int)(lon*1E6)));
System.out.println(lon);
System.out.println(lat);
} while (trackCursor.moveToNext());
}
答案 1 :(得分:5)
Cursor c=null;
c=......;
try {
if (c!=null) {
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
}
}
} finally {
if (c!=null) {
c.close();
}
}
答案 2 :(得分:4)
您实际上是跳过第一个值,而不是最后一个。
trackCursor.moveToFirst();
while (trackCursor.moveToNext()) {
第一次进入while循环时,你指的是第二行。
答案 3 :(得分:3)
您刚刚执行了查询。你能不能摆脱moveToFirst()?
public void loadTrack() {
SQLiteDatabase db1 = waypoints.getWritableDatabase();
Cursor trackCursor = db1.query(TABLE_NAME, FROM, "trackidfk=1", null, null, null,ORDER_BY);
while (trackCursor.moveToNext()) {
Double lat = trackCursor.getDouble(2);
Double lon = trackCursor.getDouble(1);
//overlay.addGeoPoint( new GeoPoint( (int)(lat*1E6), (int)(lon*1E6)));
System.out.println(lon);
System.out.println(lat);
}
}