Saturday, November 1, 2014

BookShelf app: adding book cover

In this tutorial we will add a field to our model and the database.




Step 1: Add coverUrl field

public class Book {
   private int id;
   private String title;
   private String author;
   private String isbn;
   private String coverUrl;



Step 2: Create getters and setters.

Step 3: increment database version since we are modifying the structure:

 private static final int DATABASE_VERSION = 2;


Step 4: in class BookSqlHelper add field

private static final String FIELD_COVER_URL = "cover_url";
   private static final String[] COLUMNS = { //
   FIELD_ID, // 0
         FIELD_TITLE, //  1
         FIELD_AUTHOR, // 2
         FIELD_ISBN, // 3
         FIELD_LOCATION, // 4
         FIELD_COVER_URL // 5   };


Step 5: Change all CRUD methods.

for example:


   @Override
   public int update(Book book) {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues values = new ContentValues();
      values.put(FIELD_TITLE, book.getTitle());
      values.put(FIELD_AUTHOR, book.getAuthor());
      values.put(FIELD_ISBN, book.getIsbn());
      values.put(FIELD_LOCATION, book.getLocation());
      values.put(FIELD_COVER_URL, book.getCoverUrl());