简体   繁体   English

ArrayList.add()无法正常工作

[英]ArrayList.add() not working correctly

So I have a function that gets some coordinates from a sql database and stores them in an arraylist. 因此,我有一个从sql数据库获取一些坐标并将其存储在arraylist中的函数。 I am printing the values as I read them from the cursor and they are good. 当我从光标读取值时,我正在打印这些值,它们很好。 However when I immediately after loop through and print out the values I stored in the array list, all the values seem to be set to the last value I added to the arraylist. 但是,当我立即遍历并打印出存储在数组列表中的值时,所有值似乎都设置为我添加到数组列表中的最后一个值。 I am doing something wrong here? 我在这里做错了吗?

code: 码:

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

The following is the log printout from the above code 以下是上面代码的日志打印输出

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

Try reinstantiating myCord. 尝试重新实例化myCord。 It looks like a simple reassignment problem. 它看起来像一个简单的重新分配问题。 Pays off to learn about pointers, even with Java! 即使使用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());

Need to construct new object for every iteration, instead of modifying the same one, as follows: 需要为每次迭代构造新的对象,而不是修改相同的对象,如下所示:

do {
     myCoord = new Coord();

Don't declare it each loop iteration, only reinstantiate 不要在每次循环迭代时都声明它,而只是重新实例化

That's because you add the same object to the array over and over, each time overwriting its value! 那是因为您一次又一次地向数组添加相同的对象,每次覆盖其值! Try creating a new object each time, an immutable object if you can. 尝试每次创建一个新对象,如果可以的话,创建一个不变的对象

your are initialise this code in wrong please. 请错误地初始化此代码。 use this code 使用此代码

ArrayList<Uri> ImageList = new ArrayList<Uri>(); 

inside

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM