コード例 #1
0
  /*
   * Updating a todo
   */
  public int updateToDo(Todo todo) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TODO, todo.getNote());
    values.put(KEY_STATUS, todo.getStatus());

    // updating row
    return db.update(
        TABLE_TODO, values, KEY_ID + " = ?", new String[] {String.valueOf(todo.getId())});
  }
コード例 #2
0
  /** getting all todos under single tag */
  public List<Todo> getAllToDosByTag(String tag_name) {
    List<Todo> todos = new ArrayList<Todo>();

    String selectQuery =
        "SELECT  * FROM "
            + TABLE_TODO
            + " td, "
            + TABLE_TAG
            + " tg, "
            + TABLE_TODO_TAG
            + " tt WHERE tg."
            + KEY_TAG_NAME
            + " = '"
            + tag_name
            + "'"
            + " AND tg."
            + KEY_ID
            + " = "
            + "tt."
            + KEY_TAG_ID
            + " AND td."
            + KEY_ID
            + " = "
            + "tt."
            + KEY_TODO_ID;

    Log.e(LOG, selectQuery);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
      do {
        Todo td = new Todo();
        td.setId(c.getInt((c.getColumnIndex(KEY_ID))));
        td.setNote((c.getString(c.getColumnIndex(KEY_TODO))));
        td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));

        // adding to todo list
        todos.add(td);
      } while (c.moveToNext());
    }

    return todos;
  }
コード例 #3
0
  /*
   * Creating a todo
   */
  public long createToDo(Todo todo, long[] tag_ids) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TODO, todo.getNote());
    values.put(KEY_STATUS, todo.getStatus());
    values.put(KEY_CREATED_AT, getDateTime());

    // insert row
    long todo_id = db.insert(TABLE_TODO, null, values);

    // insert tag_ids
    for (long tag_id : tag_ids) {
      createTodoTag(todo_id, tag_id);
    }

    return todo_id;
  }
コード例 #4
0
  /*
   * get single todo
   */
  public Todo getTodo(long todo_id) {
    SQLiteDatabase db = this.getReadableDatabase();

    String selectQuery = "SELECT  * FROM " + TABLE_TODO + " WHERE " + KEY_ID + " = " + todo_id;

    Log.e(LOG, selectQuery);

    Cursor c = db.rawQuery(selectQuery, null);

    if (c != null) c.moveToFirst();

    Todo td = new Todo();
    td.setId(c.getInt(c.getColumnIndex(KEY_ID)));
    td.setNote((c.getString(c.getColumnIndex(KEY_TODO))));
    td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));

    return td;
  }
コード例 #5
0
  /*
   * Deleting a tag
   */
  public void deleteTag(Tag tag, boolean should_delete_all_tag_todos) {
    SQLiteDatabase db = this.getWritableDatabase();

    // before deleting tag
    // check if todos under this tag should also be deleted
    if (should_delete_all_tag_todos) {
      // get all todos under this tag
      List<Todo> allTagToDos = getAllToDosByTag(tag.getTagName());

      // delete all todos
      for (Todo todo : allTagToDos) {
        // delete todo
        deleteToDo(todo.getId());
      }
    }

    // now delete the tag
    db.delete(TABLE_TAG, KEY_ID + " = ?", new String[] {String.valueOf(tag.getId())});
  }
コード例 #6
0
  /** getting all todos */
  public List<Todo> getAllToDos() {
    List<Todo> todos = new ArrayList<Todo>();
    String selectQuery = "SELECT  * FROM " + TABLE_TODO;

    Log.e(LOG, selectQuery);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
      do {
        Todo td = new Todo();
        td.setId(c.getInt((c.getColumnIndex(KEY_ID))));
        td.setNote((c.getString(c.getColumnIndex(KEY_TODO))));
        td.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));

        // adding to todo list
        todos.add(td);
      } while (c.moveToNext());
    }

    return todos;
  }