Ejemplo n.º 1
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    // Verify columns requested in projection exist.
    checkColumns(projection);

    // Build the sql query.
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(StocksTable.TABLE_STOCKS);
    int uriType = myUriMatcher.match(uri);
    switch (uriType) {
      case STOCKS:
        break;
      case STOCKS_ID:
        queryBuilder.appendWhere(StocksTable.COLUMN_ID + "=" + uri.getLastPathSegment());
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    // Query the database.
    SQLiteDatabase db = database.getWritableDatabase();
    Cursor cursor =
        queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
    // Watches for changes to the data in the database for the query.
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
  }
Ejemplo n.º 2
0
  @Override
  public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    SQLiteDatabase db = database.getWritableDatabase();
    int uriType = myUriMatcher.match(uri);
    int rowsUpdated;
    switch (uriType) {
      case STOCKS:
        rowsUpdated = db.update(StocksTable.TABLE_STOCKS, values, selection, selectionArgs);
        break;
      case STOCKS_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
          // No selection criteria.
          rowsUpdated =
              db.update(StocksTable.TABLE_STOCKS, values, StocksTable.COLUMN_ID + "=" + id, null);
        } else {
          rowsUpdated =
              db.update(
                  StocksTable.TABLE_STOCKS,
                  values,
                  StocksTable.COLUMN_ID + "=" + id + " and " + selection,
                  selectionArgs);
        }
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    // Notify the observer that a row has changed to allow for updates.
    getContext().getContentResolver().notifyChange(uri, null);
    // Return the number of rows that have been deleted.
    return rowsUpdated;
  }
Ejemplo n.º 3
0
  @Override
  public Uri insert(Uri uri, ContentValues values) {
    SQLiteDatabase db = database.getWritableDatabase();
    int uriType = myUriMatcher.match(uri);
    long id;
    switch (uriType) {
      case STOCKS:
        // Insert a stock symbol.
        id = db.insertOrThrow(StocksTable.TABLE_STOCKS, null, values);
        break;
      default:
        throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    // Notify the observer that a row has changed to allow for updates.
    getContext().getContentResolver().notifyChange(uri, null);
    // Return the URI for the entry just added to the database.
    return Uri.parse(BASE_PATH + "/" + id);
  }