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:

[javascript]
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}

compile ‘com.michaelpardo:activeandroid:3.1.0-SNAPSHOT’
[/javascript]

Configuration

Add some global settings to AndroidManifest.xml:

[xml]
<manifest …>
<application android:name="com.activeandroid.app.Application" …>


<meta-data
android:name="AA_DB_NAME"
android:value="feeditems.db" />
<meta-data
android:name="AA_DB_VERSION"
android:value="1" />
<meta-data
android:name="AA_MODELS"
android:value="com.androidnames.data.FeedItem" />

</application>
</manifest>
[/xml]

Note: You must use the com.activeandroid.app.Application as your application class ( see android:name=”com.activeandroid.app.Application” above). In case you use a custom application class, extend com.activeandroid.app.Application. 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, “com.androidnames.data.FeedItem, com.androidnames.data.FeedItem2, com.androidnames.data.FeedItem3”

Initialize ActiveAndroid:

[java]
public class MainActivity extends AppCompatActivity{
@Override
public void onCreate() {
super.onCreate();
ActiveAndroid.initialize(getApplication());
}
}
[/java]

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.

[java]
@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() {
super();
}
//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();
}

}
[/java]

CRUD Operations

1) Add an item
[java]
FeedItem item = new FeedItem();
item.guid = "guid1";
item.title = "Title 1";
item.description = "Description 1";
item.author = "Author 1";
item.url = "https://www.tldevtech.com/1";
item.save();
[/java]
2) Add items in batch
To insert multiple records at the same time you should use transactions to increase speed.
[java]
ActiveAndroid.beginTransaction();
try {
for (int i = 0; i < 100; i++) {
FeedItem item = new FeedItem();
item.guid = "guid" + i;
item.title = "Title " + i;
item.description = "Description " + i;
item.author = "Author " + i;
item.url = "https://www.tldevtech.com/" + i;
item.save();
}
ActiveAndroid.setTransactionSuccessful();
}
finally {
ActiveAndroid.endTransaction();
}
[/java]
3) Delete an item
[java]
//load an item then delete
FeedItem item = FeedItem.load(FeedItem.class, 1);
item.delete();
//delete the item statically by id
FeedItem.delete(Item.class, 1);
//or with
new Delete().from(FeedItem.class).where("guid = ?", "guid1").execute();
[/java]

4) Select items

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

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

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.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close