光标while循环返回的每一个值,但最后 [英] Cursor while loop returning every value but the last

查看:106
本文介绍了光标while循环返回的每一个值,但最后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个while循环遍历一个游标,然后在允许输出数据库中每一个点的经度和纬度值。

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.

出于某种原因,没有返回的最后一个(或第一个取决于如果我使用Cursor.MoveToLast)光标设定的经度和纬度值。

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.

下面是我的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:信息/的System.out(10551):3.0 04-02 15:39:07.416:信息/的System.out(10551):5.0 04-02 15:39:07.416:信息/的System.out(10551):4.0 04-02 15:39:07.416:信息/的System.out(10551):5.0 04-02 15:39:07.416:信息/的System.out(10551):5.0 04-02 15:39:07.416:信息/的System.out(10551):5.0 04-02 15:39:07.416:信息/的System.out(10551):4.0 04-02 15:39:07.416:信息/的System.out(10551):4.0 04-02 15:39:07.416:信息/的System.out(10551):3.0 04-02 15:39:07.416:信息/的System.out(10551):3.0 04-02 15:39:07.416:信息/的System.out(10551):2.0 04-02 15:39:07.416:信息/的System.out(10551):2.0 04-02 15:39:07.493:信息/的System.out(10551):1.0 04-02 15:39:07.493:信息/的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/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套。

7 Sets of values, where I should be getting 8 sets.

感谢。

推荐答案

使用MoveToNext()有两个特点。它返回一个布尔值,这标志着存在的的下一个,但在同一时间它会开始移动光标。

moveToNext() has two features. It returns a boolean signifying that there is a next, but at the same time it goes ahead and moves the cursor.

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

这篇关于光标while循环返回的每一个值,但最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆