Ejemplo n.º 1
0
  public static void updateContentProvider(List<Car> cars) {
    Context context = CarsGuideApplication.getAppContext();
    if (cars != null) {
      for (Car car : cars) {
        ContentValues carsValues = new ContentValues();
        carsValues.put(CarContract.ID, car.getId());
        carsValues.put(CarContract.CAR_TITLE, car.getTitle());
        carsValues.put(CarContract.CAR_IMAGE_URL, car.getMakeUrl());
        carsValues.put(CarContract.CAR_URL, car.getUrl());

        Cursor carCursor =
            context
                .getContentResolver()
                .query(
                    ContentUris.withAppendedId(CarContract.CONTENT_URI, car.getId()),
                    null,
                    null,
                    null,
                    null);

        if (carCursor.moveToFirst()) {
          long id = carCursor.getLong(carCursor.getColumnIndexOrThrow(CarContract.ID));
          context
              .getContentResolver()
              .update(
                  ContentUris.withAppendedId(CarContract.CONTENT_URI, id), carsValues, null, null);
        } else {
          carsValues.put(CarContract.IS_FAVORITE, 0);
          context.getContentResolver().insert(CarContract.CONTENT_URI, carsValues);
        }

        carCursor.close();
      }
    }
  }
Ejemplo n.º 2
0
  public static void setCarFavorite(int carId, boolean isFavorite) {
    Context context = CarsGuideApplication.getAppContext();

    ContentValues carsValues = new ContentValues();
    carsValues.put(CarContract.IS_FAVORITE, isFavorite ? 1 : 0);

    Cursor carCursor =
        context
            .getContentResolver()
            .query(
                ContentUris.withAppendedId(CarContract.CONTENT_URI, carId), null, null, null, null);

    if (carCursor.moveToFirst()) {
      context
          .getContentResolver()
          .update(
              ContentUris.withAppendedId(CarContract.CONTENT_URI, carId), carsValues, null, null);
    }

    carCursor.close();
  }