Ejemplo n.º 1
0
 /**
  * Updates or insert a new a category
  *
  * @param category Category to be updated or inserted
  * @return Rows affected or new inserted category id
  */
 public Category updateCategory(Category category) {
   ContentValues values = new ContentValues();
   values.put(
       KEY_CATEGORY_ID,
       category.getId() != null ? category.getId() : Calendar.getInstance().getTimeInMillis());
   values.put(KEY_CATEGORY_NAME, category.getName());
   values.put(KEY_CATEGORY_DESCRIPTION, category.getDescription());
   values.put(KEY_CATEGORY_COLOR, category.getColor());
   getDatabase(true)
       .insertWithOnConflict(
           TABLE_CATEGORY, KEY_CATEGORY_ID, values, SQLiteDatabase.CONFLICT_REPLACE);
   return category;
 }
Ejemplo n.º 2
0
  public int getCategorizedCount(Category category) {
    int count = 0;
    String sql =
        "SELECT COUNT(*)"
            + " FROM "
            + TABLE_NOTES
            + " WHERE "
            + KEY_CATEGORY
            + " = "
            + category.getId();

    Cursor cursor = null;
    try {
      cursor = getDatabase().rawQuery(sql, null);

      // Looping through all rows and adding to list
      if (cursor.moveToFirst()) {
        count = cursor.getInt(0);
      }

    } finally {
      if (cursor != null) cursor.close();
    }
    return count;
  }
Ejemplo n.º 3
0
  /**
   * Deletion of a category
   *
   * @param category Category to be deleted
   * @return Number 1 if category's record has been deleted, 0 otherwise
   */
  public long deleteCategory(Category category) {
    long deleted;

    SQLiteDatabase db = getDatabase(true);
    // Un-categorize notes associated with this category
    ContentValues values = new ContentValues();
    values.put(KEY_CATEGORY, "");

    // Updating row
    db.update(
        TABLE_NOTES,
        values,
        KEY_CATEGORY + " = ?",
        new String[] {String.valueOf(category.getId())});

    // Delete category
    deleted =
        db.delete(
            TABLE_CATEGORY,
            KEY_CATEGORY_ID + " = ?",
            new String[] {String.valueOf(category.getId())});
    return deleted;
  }