コード例 #1
0
  @Override
  public Uri insert(Uri uri, ContentValues values) {
    // Find matching path.
    final int match = sUriMatcher.match(uri);

    // Avoid the expensive string concatenation below if not loggable
    if (BuildConfig.DEBUG) {
      Log.v(TAG, "insert(uri=" + uri + ", values=" + values.toString() + ")");
    }

    // Get the database and run the insert
    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    switch (match) {
      case PAYMENTS:
        {
          db.insertOrThrow(AppDatabase.Tables.PAYMENTS, null, values);
          getContext().getContentResolver().notifyChange(uri, null);
          // Generate Uri with remote id.
          return AppContract.Payments.buildUri(values.getAsString(AppContract.Payments.ID));
        }
      default:
        {
          throw new UnsupportedOperationException("Unknown insert uri: " + uri);
        }
    }
  }
コード例 #2
0
 /**
  * Build a simple {@link SelectionBuilder} to match the requested {@link android.net.Uri}. This is
  * usually enough to support {@link #insert}, {@link #update}, and {@link #delete} operations.
  */
 private SelectionBuilder buildSimpleSelection(Uri uri, int match) {
   final SelectionBuilder builder = new SelectionBuilder();
   switch (match) {
     case PAYMENTS:
       {
         return builder.table(AppDatabase.Tables.PAYMENTS);
       }
     case PAYMENTS_ID:
       {
         final String id = AppContract.Payments.getId(uri);
         return builder
             .table(AppDatabase.Tables.PAYMENTS)
             .where(AppContract.Payments.ID + "=?", id);
       }
     default:
       {
         throw new UnsupportedOperationException("Unknown uri for " + match + ": " + uri);
       }
   }
 }