对象列表会自行覆盖

时间:2015-09-05 08:42:48

标签: android android-sqlite

即使我每次都在循环中初始化一个新对象,arraylist仍然会覆盖自己。在循环中我能够验证检索到的数据是否正确但是当我在外面打印列表时,它只显示最后一个对象已经覆盖了列表中的其他元素。

public ArrayList<CODTripsDetails> getCODTrips()
    { SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
                KEY_COD_CUST_NAME,
                KEY_COD_DEL_FARE,
                KEY_COD_DEL_ADDR,
                KEY_COD_PICKDROP_DIST,
                KEY_COD_PICKUP_ADDR,
                KEY_COD_TIMESTAMP,
                KEY_COD_ORDER_ID
        };

    Cursor cursor = db.query(TABLE_COD_DETAILS,projection,
            null, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();
    assert cursor != null;
    ArrayList<CODTripsDetails> array=new ArrayList<CODTripsDetails>();
    while (!cursor.isAfterLast()) {
        CODTripsDetails details=new CODTripsDetails();
        details.setName(cursor.getString(0));
        details.setdelFare(cursor.getString(1));
        details.setdelAddr(cursor.getString(2));
        details.setpickUpDropDist(cursor.getString(3));
        details.setpickUpAddr(cursor.getString(4));
        details.settimestamp(cursor.getString(5));
        details.setorderid(cursor.getString(6));
        Log.v("sql","sql=" +cursor.getString(3));
        array.add(details);
        cursor.moveToNext();
    }
    cursor.close();
    db.close();
    Log.v("sql","sql1="+array.get(0).getpickUpAddr());
    Log.v("sql","sql1="+array.get(1).getpickUpAddr());

    return array;

}

提前致谢。

更新代码:

这是我为检索和添加数据而编写的整个mysqlitehelper类。

  public class MySQLiteHelper extends SQLiteOpenHelper {

        ArrayList<CODTripsDetails> array=new ArrayList<CODTripsDetails>();

        private static final String TABLE_EARNINGS = "Earnings";
        private static final String TABLE_RIDER = "Rider";
        private static final String TABLE_ORDER_DETAIL = "OrderDetail";
        private static final String TABLE_DELIVERY_DETAIL = "DeliveryDetail";
        private static final String TABLE_PICKUP_DETAIL = "PickUpDetail";
        private static final String TABLE_REJ_DETAILS="RejectedTripDetails";
        private static final String TABLE_COD_DETAILS="CODTripDetails";
        private static final String TABLE_COD_ORDER_DETAILS="CODOrderDetails";
        private static final String TABLE_ONLINE_DETAILS="OnlineTripDetails";
        private static final String TABLE_ONLINE_ORDER_DETAILS="OnlineOrderDetails";
        private static final String TABLE_PREV_EARNINGS = "PrevEarnings";
        private static final String TABLE_PREV_REJ_DETAILS="RejectedTripDetails";
        private static final String TABLE_PREV_COD_DETAILS="CODTripDetails";
        private static final String 

TABLE_PREV_COD_ORDER_DETAILS="CODOrderDetails";
    private static final String TABLE_PREV_ONLINE_DETAILS="OnlineTripDetails";
    private static final String TABLE_PREV_ONLINE_ORDER_DETAILS="OnlineOrderDetails";

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "Rider";

    private static final String KEY_ID = "id";
    private static final String KEY_NAME="riderId";
    private static final String KEY_TOTAL_EARNING = "totalearnings";
    private static final String KEY_TOTAL_TRIPS = "totaltrips";
    private static final String KEY_RECEIVABLE_OR_PAYABLE = "payableorreceivable";
    private static final String KEY_COD_TRIPS = "codtrips";
    private static final String KEY_COD_FARE = "codfare";
    private static final String KEY_ONLINE_FARE = "onlinefare";
    private static final String KEY_ONLINE_TRIPS = "onlinetrips";
    private static final String KEY_KM_EXTRA="kmextra";
    private static final String KEY_DECLINED_FARE="declinedfare";
    private static final String KEY_DECLINED_TRIPS="declinedtrips";
    private static final String KEY_PICK_UP_EXTRA="pickupextraamt";
    private static final String KEY_INCENTIVE="incentive";
    private static final String KEY_COMMISSION="commission";
    private static final String KEY_SERVICE_TAX="servicetax";
    private static final String KEY_TOTAL_LOGIN_TIME="totalLoginTimeDetails";

    private static final String KEY_USER_ID="userid";
    private static final String KEY_USER_BLOCKED="blocked";
    private static final String KEY_USER_NAME="userName";
    private static final String KEY_USER_NUM="userNum";
    private static final String KEY_USER_IMAGE="userImage";


    private static final String KEY_ITEM_NAME="itemName";
    private static final String KEY_ITEM_QUANTITY="itemQty";
    private static final String KEY_ITEM_ORDER_ID="itemOrderId";


    private static final String KEY_DELIVERY_FARE="delFare";
    private static final String KEY_DELIVERY_ADDR="delAddr";
    private static final String KEY_DELIVERY_CODE="delCode";
    private static final String KEY_DELIVERY_LAT="delLat";
    private static final String KEY_DELIVERY_LNG="delLng";
    private static final String KEY_DELIVERY_TIME="delTime";
    private static final String KEY_DELIVERY_CONTACT_ADDR="delContactAddr";
    private static final String KEY_DELIVERY_NAME="delContactName";
    private static final String KEY_DELIVERY_NUM="delContactNum";

    private static final String KEY_CUST_NAME="custName";
    private static final String KEY_CUST_ID="custId";
    private static final String KEY_PICKUP_DIST="pickUpDis";
    private static final String KEY_PICKUP_DROP_DIST="pickUpDropDis";
    private static final String KEY_PICKUP_LAT="pickUpLat";
    private static final String KEY_PICKUP_LNG="pickUpLng";
    private static final String KEY_PICKUP_CONTACT_NUM="pickUpContactNum";
    private static final String KEY_PICKUP_CONTACT_NAME="pickUpContactName";
    private static final String KEY_PICKUP_ADDR="pickUpAddr";
    private static final String KEY_PAYMENT_MODE="paymentMode";
    private static final String KEY_ORDER_TIMESTAMP="timestamp";
    private static final String KEY_ORDER_ID="ordId";
    private static final String KEY_ORDER_TYPE="ordType";
    private static final String KEY_PICK_CONTACT_ADDR="pickContactAddr";


    private static final String KEY_REJ_CUST_NAME="rejCustName";
    private static final String KEY_REJ_TIMESTAMP="rejTimestamp";
    private static final String KEY_REJ_ORDER_ID="orderId";

    private static final String KEY_COD_CUST_NAME="custName";
    private static final String KEY_COD_DEL_FARE="delFare";
    private static final String KEY_COD_DEL_ADDR="delAddr";
    private static final String KEY_COD_PICKDROP_DIST="pickuDropDist";
    private static final String KEY_COD_PICKUP_ADDR="pickupAddr";
    private static final String KEY_COD_TIMESTAMP="timestamp";
    private static final String KEY_COD_ORDER_ID="orderId";

    private static final String KEY_ONLINE_CUST_NAME="custName";
    private static final String KEY_ONLINE_DEL_FARE="delFare";
    private static final String KEY_ONLINE_DEL_ADDR="delAddr";
    private static final String KEY_ONLINE_PICKDROP_DIST="pickuDropDist";
    private static final String KEY_ONLINE_PICKUP_ADDR="pickupAddr";
    private static final String KEY_ONLINE_TIMESTAMP="timestamp";
    private static final String KEY_ONLINE_ORDER_ID="orderId";

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_EARNINGS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_EARNINGS + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT," + KEY_TOTAL_EARNING + " TEXT," + KEY_TOTAL_TRIPS + " TEXT,"+ KEY_RECEIVABLE_OR_PAYABLE + " TEXT," + KEY_COD_TRIPS + " TEXT," + KEY_COD_FARE + " TEXT," + KEY_KM_EXTRA + " TEXT," + KEY_ONLINE_FARE + " TEXT," + KEY_ONLINE_TRIPS + " TEXT," + KEY_DECLINED_FARE + " TEXT," + KEY_DECLINED_TRIPS + " TEXT," + KEY_PICK_UP_EXTRA + " TEXT," + KEY_INCENTIVE + " TEXT," + KEY_COMMISSION + " TEXT," + KEY_SERVICE_TAX + " TEXT," + KEY_TOTAL_LOGIN_TIME + " TEXT" +" ) ";
        db.execSQL(CREATE_EARNINGS_TABLE);
        String CREATE_RIDERS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_RIDER + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_USER_ID + " TEXT," + KEY_USER_BLOCKED + " TEXT," + KEY_USER_NAME + " TEXT," + KEY_USER_NUM + " TEXT," + KEY_USER_IMAGE + " TEXT" + " ) ";
        db.execSQL(CREATE_RIDERS_TABLE);
        String CREATE_ORDER_DETAILS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_ORDER_DETAIL + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_ITEM_NAME + " TEXT," + KEY_ITEM_QUANTITY + " TEXT" + " ) ";
        db.execSQL(CREATE_ORDER_DETAILS_TABLE);
        String CREATE_DELIVERY_LOCATION_TABLE= "CREATE TABLE IF NOT EXISTS " + TABLE_DELIVERY_DETAIL + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_DELIVERY_FARE + " TEXT," + KEY_DELIVERY_ADDR + " TEXT,"+ KEY_DELIVERY_CODE + " TEXT," + KEY_DELIVERY_LAT + " TEXT," + KEY_DELIVERY_LNG + " TEXT," + KEY_DELIVERY_TIME + " TEXT," + KEY_DELIVERY_CONTACT_ADDR + " TEXT," + KEY_DELIVERY_NAME + " TEXT," + KEY_DELIVERY_NUM + " TEXT" + " ) ";
        db.execSQL(CREATE_DELIVERY_LOCATION_TABLE);
        String CREATE_PICKUP_LOCATION_TABLE= "CREATE TABLE IF NOT EXISTS " + TABLE_PICKUP_DETAIL + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_CUST_NAME+ " TEXT," + KEY_CUST_ID + " TEXT," + KEY_ORDER_TIMESTAMP + " TEXT," + KEY_ORDER_ID + " TEXT," + KEY_ORDER_TYPE + " TEXT," + KEY_PAYMENT_MODE + " TEXT," + KEY_PICK_CONTACT_ADDR + " TEXT," + KEY_PICKUP_CONTACT_NAME + " TEXT," + KEY_PICKUP_CONTACT_NUM + " TEXT," + KEY_PICKUP_ADDR + " TEXT," + KEY_PICKUP_DIST + " TEXT," + KEY_PICKUP_DROP_DIST + " TEXT," + KEY_PICKUP_LAT + " TEXT," + KEY_PICKUP_LNG + " TEXT" + " ) ";
        db.execSQL(CREATE_PICKUP_LOCATION_TABLE);
        String CREATE_REJ_DETAILS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_REJ_DETAILS + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_REJ_CUST_NAME + " TEXT," + KEY_REJ_TIMESTAMP + " TEXT," + KEY_REJ_ORDER_ID + " TEXT" + " ) ";
        db.execSQL(CREATE_REJ_DETAILS_TABLE);
        String CREATE_COD_DETAILS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_COD_DETAILS + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_COD_CUST_NAME + " TEXT," + KEY_COD_DEL_FARE + " TEXT," + KEY_COD_DEL_ADDR + " TEXT," +  KEY_COD_PICKDROP_DIST + " TEXT," + KEY_COD_PICKUP_ADDR + " TEXT," + KEY_COD_TIMESTAMP + " TEXT," + KEY_COD_ORDER_ID + " TEXT" + " ) ";
        db.execSQL(CREATE_COD_DETAILS_TABLE);
        String CREATE_ONLINE_DETAILS_PAGE = "CREATE TABLE IF NOT EXISTS " + TABLE_ONLINE_DETAILS + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_ONLINE_CUST_NAME + " TEXT," + KEY_ONLINE_DEL_FARE + " TEXT," + KEY_ONLINE_DEL_ADDR + " TEXT," +  KEY_ONLINE_PICKDROP_DIST + " TEXT," + KEY_ONLINE_PICKUP_ADDR + " TEXT," + KEY_ONLINE_TIMESTAMP + " TEXT," + KEY_ONLINE_ORDER_ID + " TEXT" + " ) ";
        db.execSQL(CREATE_ONLINE_DETAILS_PAGE);
            String CREATE_ONLINE_ORDER_DETAILS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_ONLINE_ORDER_DETAILS + " ( "
                    + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_ITEM_NAME + " TEXT," + KEY_ITEM_QUANTITY + " TEXT," + KEY_ITEM_ORDER_ID + " TEXT" + " ) ";
        db.execSQL(CREATE_ONLINE_ORDER_DETAILS_TABLE);
        String CREATE_COD_ORDER_DETAILS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_COD_ORDER_DETAILS + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_ITEM_NAME + " TEXT," + KEY_ITEM_QUANTITY + " TEXT," + KEY_ITEM_ORDER_ID + " TEXT" + " ) ";
        db.execSQL(CREATE_COD_ORDER_DETAILS_TABLE);

        ///////////////////////////

        Log.v("table","tablescreated");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i2) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PICKUP_DETAIL);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_EARNINGS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_DELIVERY_DETAIL);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_RIDER);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDER_DETAIL);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_REJ_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_COD_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_COD_ORDER_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ONLINE_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ONLINE_ORDER_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PREV_EARNINGS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PREV_REJ_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PREV_COD_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PREV_COD_ORDER_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PREV_ONLINE_DETAILS);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PREV_ONLINE_ORDER_DETAILS);
        onCreate(db);
    }

    public void addDeliveryDetails(String delFare,String delAddr,String delCode,String delLat,String delLong,String delTime,String delContactAddr,String delName,String delNum)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues value=new ContentValues();
        value.put(KEY_DELIVERY_FARE,delFare);
        value.put(KEY_DELIVERY_ADDR,delAddr);
        value.put(KEY_DELIVERY_CODE,delCode);
        value.put(KEY_DELIVERY_LAT,delLat);
        value.put(KEY_DELIVERY_LNG,delLong);
        value.put(KEY_DELIVERY_TIME,delTime);
        value.put(KEY_DELIVERY_CONTACT_ADDR,delContactAddr);
        value.put(KEY_DELIVERY_NAME,delName);
        value.put(KEY_DELIVERY_NUM,delNum);
        db.insert(TABLE_DELIVERY_DETAIL, null, value);
        db.close();
    }

    public void addPickUpDetails(String custName,String custId,String time,String orderId,String orderType,String paymentMode,String pickcontactaddr,String pickcontactname,String pickcontactnum,String pickupaddr,String pickupdist,String pickupdropdist,String pickuplat,String pickuplng )
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues value=new ContentValues();
        value.put(KEY_CUST_NAME,custName);
        value.put(KEY_CUST_ID,custId);
        value.put(KEY_ORDER_TIMESTAMP,time);
        value.put(KEY_ORDER_ID,orderId);
        value.put(KEY_ORDER_TYPE,orderType);
        value.put(KEY_PAYMENT_MODE,paymentMode);
        value.put(KEY_PICK_CONTACT_ADDR,pickcontactaddr);
        value.put(KEY_PICKUP_CONTACT_NAME,pickcontactname);
        value.put(KEY_PICKUP_CONTACT_NUM,pickcontactnum);
        value.put(KEY_PICKUP_ADDR,pickupaddr);
        value.put(KEY_PICKUP_DIST,pickupdist);
        value.put(KEY_PICKUP_DROP_DIST,pickupdropdist);
        value.put(KEY_PICKUP_LAT,pickuplat);
        value.put(KEY_PICKUP_LNG,pickuplng);
        db.insert(TABLE_PICKUP_DETAIL, null, value);
        db.close();
    }


    public String getCustId()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
                KEY_CUST_ID
        };
        Cursor cursor = db.query(TABLE_PICKUP_DETAIL,projection,
                null, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        assert cursor != null;
        String custId=cursor.getString(0);
        cursor.close();
        db.close();
        return custId;
    }



    public String getPickUpDist()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
                KEY_PICKUP_DIST
        };
        Cursor cursor = db.query(TABLE_PICKUP_DETAIL,projection,
                null, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        assert cursor != null;
        String pickupdist=cursor.getString(0);
        cursor.close();
        db.close();
        return pickupdist;
    }

    public String getPickUpDropDist()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
                KEY_PICKUP_DROP_DIST
        };
        Cursor cursor = db.query(TABLE_PICKUP_DETAIL,projection,
                null, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        assert cursor != null;
        String pickupdropdist=cursor.getString(0);
        cursor.close();
        db.close();
        return pickupdropdist;
    }

    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }







    public void addCODOrders(String name,String delFare,String delAddr,String pickupdropDist,String pickupAddr,String timestamp,String orderid)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_COD_CUST_NAME,name);
        values.put(KEY_COD_DEL_FARE,delFare);
        values.put(KEY_COD_DEL_ADDR,delAddr);
        values.put(KEY_COD_PICKDROP_DIST,pickupdropDist);
        values.put(KEY_COD_PICKUP_ADDR,pickupAddr);
        values.put(KEY_COD_TIMESTAMP,timestamp);
        values.put(KEY_COD_ORDER_ID,orderid);
        db.insert(TABLE_COD_DETAILS, null, values);
        db.close();
    }

    public void addOnlineOrders(String name,String delFare,String delAddr,String pickupAddr,String pickupdropDist,String timestamp,String orderid)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_ONLINE_CUST_NAME,name);
        values.put(KEY_ONLINE_DEL_FARE,delFare);
        values.put(KEY_ONLINE_DEL_ADDR,delAddr);
        values.put(KEY_ONLINE_PICKDROP_DIST,pickupdropDist);
        values.put(KEY_ONLINE_PICKUP_ADDR,pickupAddr);
        values.put(KEY_ONLINE_TIMESTAMP,timestamp);
        values.put(KEY_ONLINE_ORDER_ID,orderid);
        db.insert(TABLE_ONLINE_DETAILS, null, values);
        db.close();
    }


    public ArrayList<CODTripsDetails> getCODTrips()
    { SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
                KEY_COD_CUST_NAME,
                KEY_COD_DEL_FARE,
                KEY_COD_DEL_ADDR,
                KEY_COD_PICKDROP_DIST,
                KEY_COD_PICKUP_ADDR,
                KEY_COD_TIMESTAMP,
                KEY_COD_ORDER_ID
        };

        Cursor cursor = db.query(TABLE_COD_DETAILS,projection,
                null, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        assert cursor != null;

        while (!cursor.isAfterLast()) {
            CODTripsDetails details=new CODTripsDetails();
            details.setName(cursor.getString(0));
            details.setdelFare(cursor.getString(1));
            details.setdelAddr(cursor.getString(2));
            details.setpickUpDropDist(cursor.getString(3));
            details.setpickUpAddr(cursor.getString(4));
            details.settimestamp(cursor.getString(5));
            details.setorderid(cursor.getString(6));
            Log.v("sql","sql=" +cursor.getString(3));
            array.add(details);
            cursor.moveToNext();
        }
        cursor.close();
        db.close();
        Log.v("sql","sql1="+array.get(0).getpickUpAddr());
        Log.v("sql","sql1="+array.get(1).getpickUpAddr());

        return array;

    }












    public CODTripsDetails[] getOnlineTrips()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
                KEY_ONLINE_CUST_NAME,
                KEY_ONLINE_DEL_FARE,
                KEY_ONLINE_DEL_ADDR,
                KEY_ONLINE_PICKDROP_DIST,
                KEY_ONLINE_PICKUP_ADDR,
                KEY_ONLINE_TIMESTAMP,
                KEY_ONLINE_ORDER_ID
        };
        int i=0;
        Cursor cursor = db.query(TABLE_ONLINE_DETAILS,projection,
                null, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        assert cursor != null;
        CODTripsDetails[] array=new CODTripsDetails[cursor.getCount()];
        while (!cursor.isAfterLast()) {
            array[i]=new CODTripsDetails(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6));
            i++;
            cursor.moveToNext();
        }
        cursor.close();
        db.close();
        return array;
    }

    public void addCODORderDetails(String itemName,String itemQuantity,String orderid)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_ITEM_NAME,itemName);
        values.put(KEY_ITEM_QUANTITY,itemQuantity);
        values.put(KEY_ITEM_ORDER_ID,orderid);
        db.insert(TABLE_COD_ORDER_DETAILS, null, values);
        db.close();
    }

    public void addOnlineORderDetails(String itemName,String itemQuantity,String orderid)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_ITEM_NAME,itemName);
        values.put(KEY_ITEM_QUANTITY,itemQuantity);
        values.put(KEY_ITEM_ORDER_ID,orderid);
        db.insert(TABLE_ONLINE_ORDER_DETAILS, null, values);
        db.close();
    }

    public OrderDetail[] getCODRderDetails(String orderid) {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
                KEY_ITEM_NAME,
                KEY_ITEM_QUANTITY
        };
        int i = 0;
        String where="itemOrderId=?";
        String[] args={orderid};
        Cursor cursor = db.query(TABLE_COD_ORDER_DETAILS, projection,
                where, args, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        assert cursor != null;
        OrderDetail[] array = new OrderDetail[cursor.getCount()];
        while (!cursor.isAfterLast()) {
            array[i] = new OrderDetail(cursor.getString(0), cursor.getString(1));
            i++;
            cursor.moveToNext();
        }
        cursor.close();
        db.close();
        return array;
    }


    public OrderDetail[] getOnlineOrderDetails(String orderid) {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
                KEY_ITEM_NAME,
                KEY_ITEM_QUANTITY
        };
        int i = 0;
        String where="itemOrderId=?";
        String[] args={orderid};
        Cursor cursor = db.query(TABLE_ONLINE_ORDER_DETAILS, projection,
                where, args, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        assert cursor != null;
        OrderDetail[] array = new OrderDetail[cursor.getCount()];
        while (!cursor.isAfterLast()) {
            array[i] = new OrderDetail(cursor.getString(0), cursor.getString(1));
            i++;
            cursor.moveToNext();
        }
        cursor.close();
        db.close();
        return array;
    }



}

1 个答案:

答案 0 :(得分:1)

ArrayList<CODTripsDetails> array=new ArrayList<CODTripsDetails>();移到函数外部。在您的情况下,ArrayList<CODTripsDetails> array的范围仅在函数中,一旦函数终止,数组就会被删除。

修改

public class SomeActivity extends AppCompatActivity {
    private MySQLiteHelper db;
    private ArrayList<CODTripsDetails> array=new ArrayList<CODTripsDetails>();

    @Override
    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);

        array = db.getCODTrips(); // This sets your array to data
    }
}