public void addItemToOrder(Context context, final Order order, final Item item) { final SQLiteDatabase db = this.getWritableDatabase(); final Toast errorToast = Toast.makeText(context, "Problema en el servidor", Toast.LENGTH_SHORT); final Context mContext = context; ApiClient.getRestAppApiClient(context) .addItemToOrder( order.getId(), item.getId(), 1, new Callback<com.tesis.restapp.restapp.database.Response>() { @Override public void success( com.tesis.restapp.restapp.database.Response data, retrofit.client.Response response) { if (data.wasSuccessful()) { ContentValues values = new ContentValues(); values.put(KEY_ID, data.getId()); values.put(KEY_ORDER_ID, order.getId()); values.put(KEY_ITEM_ID, item.getId()); values.put(KEY_QUANTITY, 1); values.put(KEY_PRICE, item.getPrice()); // Inserting Row db.insert(TABLE_ORDER_ITEM, null, values); itemsInOrderAdapter.notifyDataSetChanged(); ordersAdapter.aNotifyDataSetChanged(); } else { Toast.makeText(mContext, data.getMessage(), Toast.LENGTH_SHORT).show(); } db.close(); // Closing database connection } @Override public void failure(RetrofitError error) { errorToast.show(); Log.e(this.getClass().getSimpleName(), error.getMessage()); db.close(); } }); }
public void removeItemFromOrder( Context context, final Order order, final Item item, final ImageButton btn) { final SQLiteDatabase db = this.getWritableDatabase(); final Toast errorToast = Toast.makeText(context, "Problema en el servidor", Toast.LENGTH_SHORT); final Context mContext = context; String selectQuery = "SELECT * FROM " + TABLE_ORDER_ITEM + " WHERE " + TABLE_ORDER_ITEM + "." + KEY_ITEM_ID + "=" + item.getId() + " AND " + TABLE_ORDER_ITEM + "." + KEY_ORDER_ID + " =" + order.getId(); Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { ApiClient.getRestAppApiClient(context) .removeItemFromOrder( cursor.getInt(cursor.getColumnIndex(KEY_ID)), new Callback<com.tesis.restapp.restapp.database.Response>() { @Override public void success( com.tesis.restapp.restapp.database.Response apiResponse, Response response) { if (apiResponse.wasSuccessful()) { db.delete( TABLE_ORDER_ITEM, "id = ?", new String[] {String.valueOf(apiResponse.getId())}); order.removeItem(item); itemsInOrderAdapter.notifyDataSetChanged(); } else { Toast.makeText(mContext, apiResponse.getMessage(), Toast.LENGTH_SHORT).show(); btn.post( new Runnable() { @Override public void run() { btn.setVisibility(View.VISIBLE); } }); } db.close(); // Closing database connection } @Override public void failure(RetrofitError error) { errorToast.show(); db.close(); btn.post( new Runnable() { @Override public void run() { btn.setVisibility(View.VISIBLE); } }); } }); } }