java 游标while循环返回除最后一个值之外的所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2567594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cursor while loop returning every value but the last
提问by jcrowson
I am using a while loop to iterate through a cursor and then outputing the longitude and latitude values of every point within the database.
我使用 while 循环遍历游标,然后输出数据库中每个点的经度和纬度值。
For some reason it is not returning the last (or first depending on if I use Cursor.MoveToLast) set of longitude and latitude values in the cursor.
出于某种原因,它没有返回光标中的最后一组(或第一组,取决于我是否使用 Cursor.MoveToLast)经度和纬度值。
Here is my code:
这是我的代码:
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);
}
}
From this I am getting:
由此我得到:
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
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 .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 05:02 39:07.416:信息/系统输出(10551):5.0 04-02 15:39:07.416:信息/系统输出(10551):4.0 04-02 15:39:07.416:信息/系统输出(10555) : 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:316: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:信息/系统输出(10551):1.0
7 Sets of values, where I should be getting 8 sets.
7 组值,我应该得到 8 组。
Thanks.
谢谢。
回答by Parker
moveToNext() has two features. It returns a boolean signifying that there isa next, but at the same time it goes ahead and moves the cursor.
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());
}
回答by Pentium10
Cursor c=null;
c=......;
try {
if (c!=null) {
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
}
}
} finally {
if (c!=null) {
c.close();
}
}
回答by Mark B
Your skipping the first value actually, not the last.
您实际上跳过了第一个值,而不是最后一个。
trackCursor.moveToFirst();
while (trackCursor.moveToNext()) {
The first time into the while loop you are pointing at the 2nd row.
第一次进入 while 循环时,您将指向第二行。
I would convert your while loop to a do-while loop
回答by Billy Bob Bain
You just executed the query. Can't you just get rid of the moveToFirst()?
您刚刚执行了查询。你不能摆脱 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);
}
}

