2012-06-27 49 views
0

所以我有得到一些座標從一個ArrayList SQL數據庫,並將其存儲功能。我在從光標讀取值時打印這些值,它們很好。但是,當我立即循環並打印出存儲在數組列表中的值時,所有值似乎都設置爲我添加到數組列表中的最後一個值。我在這裏做錯了什麼?ArrayList.add()工作不正常

代碼:

public ArrayList<Coord> getCoordMarkers(int myUserId) 
{ 
    Log.d("getCoordMarkers()", "Called"); 
Coord myCoord = new Coord(); 
ArrayList<Coord> markerArray = new ArrayList<Coord>(); 
String sql = "SELECT "+ userId + " , " + timestamp + " , " + coordX + " , " + 
       coordY + " , " + coordType + " , " + coordId + " , " + coordTypeTable+ "." + 
       coordTypeDesc + " FROM " + coordTable + " JOIN " + coordTypeTable + " ON " + 
       coordTable + "." + coordType + " = " + coordTypeTable + "." + coordTypeAbbr + 
       " WHERE " + userId + " = '" + myUserId + "' AND " + coordTypeTable + "." + 
       coordTypeDesc + " != 'User Location';"; 
    // AND it is a marker 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cur = db.rawQuery(sql, null); 
    if(cur.moveToFirst()) { 
     do { 
      myCoord.userId = cur.getInt(0); 
      myCoord.timestamp = cur.getLong(1); 
      myCoord.x = cur.getDouble(2); 
      myCoord.y = cur.getDouble(3); 
      myCoord.coordType = cur.getInt(4); 
      myCoord.id = cur.getInt(5); 
      myCoord.coordTypeDesc = cur.getString(6); 
      markerArray.add(myCoord); 
      Log.d("getCoordMarkers()", "X: " + myCoord.x + " Y: " + myCoord.y); 
     } while(cur.moveToNext()); 
    } 
    cur.close(); 
    db.close(); 
    for(int i = 0; i < markerArray.size(); i++) 
    { 
     Coord myCoord2 = markerArray.get(i); 
     Log.d("Test Marker", "I: "+ i + " X: " + myCoord2.x + " Y: "+ myCoord2.y); 

    } 
    return markerArray; 
} 

以下是從上述代碼

06-27 01:46:06.588: DEBUG/getCoordMarkers()(541): Called 
06-27 01:46:06.638: DEBUG/getCoordMarkers()(541): X: -122.093451 Y: 37.420599 
06-27 01:46:06.638: DEBUG/getCoordMarkers()(541): X: -122.093451 Y: 37.420599 
06-27 01:46:06.648: DEBUG/getCoordMarkers()(541): X: -122.094695 Y: 37.419679 
06-27 01:46:06.648: DEBUG/getCoordMarkers()(541): X: -122.094695 Y: 37.419679 
06-27 01:46:06.648: DEBUG/getCoordMarkers()(541): X: -122.090104 Y: 37.423633 
06-27 01:46:06.658: DEBUG/getCoordMarkers()(541): X: -122.090104 Y: 37.423633 
06-27 01:46:06.658: DEBUG/getCoordMarkers()(541): X: -122.092078 Y: 37.420464 
06-27 01:46:06.658: DEBUG/getCoordMarkers()(541): X: -122.092078 Y: 37.420464 
06-27 01:46:06.658: DEBUG/getCoordMarkers()(541): X: -122.091992 Y: 37.420668 
06-27 01:46:06.668: DEBUG/getCoordMarkers()(541): X: -122.091992 Y: 37.420668 
06-27 01:46:06.668: DEBUG/getCoordMarkers()(541): X: -122.09594 Y: 37.42077 
06-27 01:46:06.678: DEBUG/getCoordMarkers()(541): X: -122.09594 Y: 37.42077 
06-27 01:46:06.678: DEBUG/getCoordMarkers()(541): X: -122.09255 Y: 37.42118 
06-27 01:46:06.678: DEBUG/getCoordMarkers()(541): X: -122.09255 Y: 37.42118 
06-27 01:46:06.688: DEBUG/getCoordMarkers()(541): X: -122.095897 Y: 37.423633 
06-27 01:46:06.698: DEBUG/getCoordMarkers()(541): X: -122.095897 Y: 37.423633 
06-27 01:46:06.708: DEBUG/Test Marker(541): I: 0 X: -122.095897 Y: 37.423633 
06-27 01:46:06.708: DEBUG/Test Marker(541): I: 1 X: -122.095897 Y: 37.423633 
06-27 01:46:06.708: DEBUG/Test Marker(541): I: 2 X: -122.095897 Y: 37.423633 
06-27 01:46:06.708: DEBUG/Test Marker(541): I: 3 X: -122.095897 Y: 37.423633 
06-27 01:46:06.718: DEBUG/Test Marker(541): I: 4 X: -122.095897 Y: 37.423633 
06-27 01:46:06.718: DEBUG/Test Marker(541): I: 5 X: -122.095897 Y: 37.423633 
06-27 01:46:06.728: DEBUG/Test Marker(541): I: 6 X: -122.095897 Y: 37.423633 
06-27 01:46:06.739: DEBUG/Test Marker(541): I: 7 X: -122.095897 Y: 37.423633 
06-27 01:46:06.739: DEBUG/Test Marker(541): I: 8 X: -122.095897 Y: 37.423633 
06-27 01:46:06.739: DEBUG/Test Marker(541): I: 9 X: -122.095897 Y: 37.423633 
06-27 01:46:06.748: DEBUG/Test Marker(541): I: 10 X: -122.095897 Y: 37.423633 
06-27 01:46:06.748: DEBUG/Test Marker(541): I: 11 X: -122.095897 Y: 37.423633 
06-27 01:46:06.748: DEBUG/Test Marker(541): I: 12 X: -122.095897 Y: 37.423633 
06-27 01:46:06.748: DEBUG/Test Marker(541): I: 13 X: -122.095897 Y: 37.423633 
06-27 01:46:06.758: DEBUG/Test Marker(541): I: 14 X: -122.095897 Y: 37.423633 
06-27 01:46:06.758: DEBUG/Test Marker(541): I: 15 X: -122.095897 Y: 37.423633 

回答

10

嘗試重新實例myCord日誌打印輸出。它看起來像一個簡單的重新分配問題。爲了學習關於指針的知識,即使使用Java!

do { 
     myCoord = new Coord(); //********** 
     myCoord.userId = cur.getInt(0); 
     myCoord.timestamp = cur.getLong(1); 
     myCoord.x = cur.getDouble(2); 
     myCoord.y = cur.getDouble(3); 
     myCoord.coordType = cur.getInt(4); 
     myCoord.id = cur.getInt(5); 
     myCoord.coordTypeDesc = cur.getString(6); 
     markerArray.add(myCoord); 
     Log.d("getCoordMarkers()", "X: " + myCoord.x + " Y: " + myCoord.y); 
     myCoord = null; //for the garbage collector ******* 
    } while(cur.moveToNext()); 
+1

謝謝你做的伎倆,我沒有意識到數組列表使用指針來分配座標。你有什麼建議在Java指針的來源?謝謝 – jgelderloos

+1

嗯,它更多的是學習指針如何工作以及Java如何隱藏它們。 Java是完全傳遞值的。下面是一個關於C語言指針的網站:http://www.cprogramming.com/tutorial/lesson6.html –

+0

我在C語言中有指針經驗,我只是沒有意識到java正在這樣做,因爲它並沒有讓它變得明顯。正在這樣做。 – jgelderloos

4

需要構建新的對象每次迭代,而不是修改同一個, 如下:

do { 
    myCoord = new Coord(); 

不要聲明它每次循環迭代,只有重新實例

+0

確保你不重新聲明變量 –

+0

@ jack57:同意。只是當它真的很大時,我發現閱讀和理解代碼更容易。 –

0

這是因爲您一次又一次地將相同的對象添加到數組中,每次都覆蓋它的值!如果可以,請嘗試每次創建一個新對象,即immutable object