@Override public ShoppingList getShoppingListFromDatabase() { database = this.getReadableDatabase(); ShoppingList list = new ShoppingList(); Cursor cursor = database.query( SQLiteHelper.TABLE_SHOPPINGLIST, null, null, null, null, null, SQLiteHelper.SHOPPINGLIST_PRODUCT + " DESC"); cursor.moveToFirst(); while (!cursor.isAfterLast()) { int id = cursor.getInt(cursor.getColumnIndex(SHOPPINGLIST_PRODUCT)); Product p = getProductById(id); if (p != null) { list.addToList(p); } cursor.moveToNext(); } cursor.close(); database.close(); return list; }
@Override public void saveShoppingListToDatabase(ShoppingList list) { database = this.getWritableDatabase(); ArrayList<Product> datalist = new ArrayList<Product>(list.getContents()); Iterator<Product> iter = datalist.iterator(); while (iter.hasNext()) { Product prod = iter.next(); createRow(prod); } database.close(); }