ActiveAndroid Tutorial: Working with the Library

ActiveAndroid is a simple-to-use Object Relational Mapper library, which maps java classes to database tables and java class member variables to the table columns. Each row in a table will become an object. This will help developer create, update, delete and select data from SQLite database using model objects without using raw SQL queries.

This post shows I add ActiveAndroid to one of my Android RSS reader app.

Add the library

First method: import .jar file

– Download the ActiveAndroid library from Github.
– Drag the jar file to the “libs” folder your Android project.

Second method: via Gradle

Put following lines to your project’s app/build.gradle:

repositories {
    maven { url "" }

compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'


Add some global settings to AndroidManifest.xml:

<manifest ...>
    <application android:name="" ...>

            android:value="feeditems.db" />
            android:value="1" />
            android:value="" />


Note: You must use the as your application class ( see android:name=”” above). In case you use a custom application class, extend This post won’t cover the 2nd method.

  • AA_DB_NAME (optional). It is database’s name. If you don’t use this setting, you have to set database’s name later when initializing ActiveAndroid.
  • AA_DB_VERSION (optional – defaults to 1). Change this setting helps notify about database’s structure change.
  • AA_MODELS (optional ). ActiveAndroid automatically looks through all your files to find your Model classes. This setting helps disable model searching, which increases the app’s performance. Use comma if you use more than one models in a project. For example, “,,”

Initialize ActiveAndroid:

public class MainActivity extends AppCompatActivity{
    public void onCreate() {

Create table and object

Now we can create a table and map its columns to a feed item’s properties. It is based on annotation like: @Column(name=”Column”) for creating column and Table(name = “Table”) for creating table.

@Table(name = "FeedItem")
public class FeedItem extends Model {

    @Column(name = "guid", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
    public String guid;
    @Column(name = "title")
    public String title;
    @Column(name = "description")
    public String description;
    @Column(name = "pubDate")
    public String pubDate;
    @Column(name = "author")
    public String author;
    @Column(name = "thumbnail")
    public String thumbnail;
    @Column(name = "url")
    public String url;

    public FeedItem() {
    //find items by guid
    public static FeedItem findByGUID(String guid){
        return new Select().from(FeedItem.class).where("guid = ?", guid).executeSingle();
    //retrieve all items
    public static List<FeedItem> getAllFeedItems() {
        return new Select().from(FeedItem.class).orderBy("guid DESC").execute();


CRUD Operations

1) Add an item

FeedItem item = new FeedItem();
item.guid = "guid1";
item.title = "Title 1";
item.description = "Description 1"; = "Author 1";
item.url = "";;

2) Add items in batch
To insert multiple records at the same time you should use transactions to increase speed.

try {
        for (int i = 0; i < 100; i++) {
            FeedItem item = new FeedItem();
            item.guid = "guid" + i;
            item.title = "Title " + i;
            item.description = "Description " + i;
   = "Author " + i;
            item.url = "" + i;
finally {

3) Delete an item

//load an item then delete
FeedItem item = FeedItem.load(FeedItem.class, 1);
//delete the item statically by id
FeedItem.delete(Item.class, 1);
//or with
new Delete().from(FeedItem.class).where("guid = ?", "guid1").execute();

4) Select items

You can use Select class to query a list of items.

List<FeedItem> items = new Select().from(FeedItem.class).orderBy("title ASC").limit(100).execute();

Another method is to create static method within model class. As you can see in the model’s code, I create 2 static methods, findByGUID(String guid) and List getAllFeedItems(), which can retrieve items based on a set of input conditions.