所以我有一个函数从sql数据库获取一些坐标并将它们存储在一个arraylist中。我正在从光标读取它时打印值,它们很好。但是,当我在循环后立即打印出存储在数组列表中的值时,所有值似乎都设置为我添加到arraylist的最后一个值。我在这里做错了什么?
代码:
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
答案 0 :(得分: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 :(得分:4)
需要为每次迭代构造新对象,而不是修改相同的对象, 如下:
do {
myCoord = new Coord();
不要在每次循环迭代时声明它,只重新实现
答案 2 :(得分:0)
那是因为你一遍又一遍地向数组中添加同一个对象,每次都要覆盖它的值!每次尝试创建一个新对象,如果可以,请immutable object。
答案 3 :(得分:0)
请错误地初始化此代码。使用此代码
ArrayList<Uri> ImageList = new ArrayList<Uri>();
内部
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ArrayList<Uri> ImageList = new ArrayList<Uri>();
if(requestCode == PICK_FILE)
{
if(resultCode == RESULT_OK)
{
if(data.getClipData()!=null)
{
int countClipData = data.getClipData().getItemCount();
int currentImageSelected = 0;
while (currentImageSelected < countClipData)
{
ImageUri = data.getClipData().getItemAt(currentImageSelected).getUri();
ImageList.add(ImageUri);
currentImageSelected = currentImageSelected +1;
}
Alert.setVisibility(View.VISIBLE);
Alert.setText("You Have Selected"+ImageList.size()+"Images");
Btn_Choose.setVisibility(View.GONE);
}
else
{
Toast.makeText(this, "Please select your car images", Toast.LENGTH_SHORT).show();
}
}
}
}
}