First meetup

21 July 2014 marked our first usergroup meetup. After having discussed starting an Android usergroup since the beginning of the year, we decided to just do it.

We decided to start small and tried to keep it fairly informal, so after a quick introduction we had 2 topics to discuss:

  • Recent developments in the Android world (By Matt)
  • Sugar ORM (By Ross)

Recent developments in the Android world

As Google IO happened recently, we mostly focused on announcements from there. The keynote is well worth watching: Google I/O 14 Keynote

We also briefly discussed:

And as everybody that attended works on Discovery Vitality applications, this was quite a big one for us:

Sugar ORM

Ross gave us a quick introduction to Sugar ORM

Sugar ORM is a very simple ORM for Android that allows you to interact with SQLite in a simple way with minimal boilerplate.

Sugar CRM focuses on two things and nothing more:

  • Database creation
  • Simple APIs to manipulate domain objects

Setup

Sugar ORM requires very little setup.

All you need to do is, specify SugarApp as your application class in AndroidManifest.xml. You do that by changing the android:name attribute of the application tag.

There’s also some optional meta tags to configure it. In your AndroidManifest.xml:

<application android:label="@string/app_name" android:icon="@drawable/icon"     android:name="com.orm.SugarApp">
    .
    .
    <meta-data android:name="DATABASE" android:value="sugar_example.db" />
    <meta-data android:name="VERSION" android:value="2" />
    <meta-data android:name="QUERY_LOG" android:value="true" />
    <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.example" />
    .
    .
</application>

Entities

You simply extend SugarRecord for all the classes that you need persisted. That’s it:

public class Book extends SugarRecord<Book> {
  String title;
  String edition;

  public Book(){}

  public Book(String title, String edition){
    this.title = title;
    this.edition = edition;
  }
}

APIs

Performing CRUD operations are very simple. Functions like save(), delete() and findById(..) are provided to make the work easy.

Save Entity:

Book book = new Book(ctx, "Title here", "2nd edition")
book.save();

Load Entity:

Book book = Book.findById(Book.class, 1);

Update Entity:

Book book = Book.findById(Book.class, 1);
book.title = "updated title here"; // modify the values
book.edition = "3rd edition";
book.save(); // updates the previous entry with new values.

Delete Entity:

Book book = Book.findById(Book.class, 1);
book.delete();

Bulk Operations:

List<Book> books = Book.listAll(Book.class);
Book.deleteAll(Book.class);