Slide 105
Slide 105 text
Traditional Android APIs
Book getBookByPublishedDate(SQLiteOpenHelper openHelper,
ZonedDateTime publishedAt) {
SQLiteDatabase db = openHelper.getReadableDatabase();
String[] projection = {
Book.TITLE,
Book.AUTHOR,
Book.PUBLISHEDAT
};
String selection = Book.PUBLISHEDAT + " = ?";
String[] selectionArgs = {FORMATTER.format(publishedAt)};
Cursor cursor = db.query(
Book.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
null);
Book book = null;
if (cursor.moveToNext()) {
String title = cursor.getString(cursor.getColumnIndexOrThrow(Book.TITLE));
String author = cursor.getString(cursor.getColumnIndexOrThrow(Book.AUTHOR));
String publishedAtStr = cursor.getString(cursor.getColumnIndexOrThrow(Book.PUBLISHEDAT));
ZonedDateTime parsed = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(publishedAtStr,
ZonedDateTime.FROM);
book = Book.create(title, author, parsed);
}
cursor.close();
return book;
}
Book getBookByPublishedDate(SQLiteOpenHelper openHelper,
ZonedDateTime publishedAt) {
SQLiteDatabase db = openHelper.getReadableDatabase();
String[] projection = {
Book.TITLE,
Book.AUTHOR,
Book.PUBLISHEDAT
};
String selection = Book.PUBLISHEDAT + " = ?";
String[] selectionArgs = {FORMATTER.format(publishedAt)};
Cursor cursor = db.query(
Book.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
null);
Book book = null;
if (cursor.moveToNext()) {
String title = cursor.getString(cursor.getColumnIndexOrThrow(Book.TITLE));
String author = cursor.getString(cursor.getColumnIndexOrThrow(Book.AUTHOR));
String publishedAtStr = cursor.getString(cursor.getColumnIndexOrThrow(Book.PUBLISHEDAT));
ZonedDateTime parsed = DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(publishedAtStr,
ZonedDateTime.FROM);
book = Book.create(title, author, parsed);
}
cursor.close();
return book;
}