The cursor while the loop returns each value, but the last

I use a while loop to iterate over the cursor and then output the longitude and latitude values โ€‹โ€‹of each point in the database.

For some reason, it does not return the last (or the first, depending on whether I use Cursor.MoveToLast) a set of longitude and latitude values โ€‹โ€‹in the cursor.

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 get:


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 Sets the values โ€‹โ€‹where I should receive 8 sets.

Thanks.

+6
java android sql sqlite cursor
source share
4 answers

moveToNext () has two functions. It returns a boolean meaning that there is another, but at the same time it goes forward 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()); } 
+22
source share
 Cursor c=null; c=......; try { if (c!=null) { for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { } } } finally { if (c!=null) { c.close(); } } 
+5
source share

Missing first value, not last.

 trackCursor.moveToFirst(); while (trackCursor.moveToNext()) { 

For the first time in a while loop, you point to the second line.

I would convert the while to a do-while loop

+4
source share

You have just completed the request. Can't you just get rid of 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); } } 
+3
source share

All Articles