-
Notifications
You must be signed in to change notification settings - Fork 2
Getting started
Let’s get started with ActiveAndroid On Steroids. The first thing you need to do, if you haven’t already done so, is download the ActiveAndroid library.
or clone it into your git project by: git submodule add https://github.com/agrosner/ActiveAndroid.git
Now that you have the ActiveAndroid library you can add it as a gradle dependency: Note I am assuming your top-level project has a subfolder called 'Libraries' and ActiveAndroid is the folder
dependencies{
compile project(':Libraries:ActiveAndroid')
}
By cloning the source, or downloading it, you can build the jar file by running ant in the root folder. Your ActiveAndroid.jar, which is what we need, will be in the dist folder.
or if you're still using Eclipse: add it to your project’s build path. We’ll assume you’re using Eclipse:
- If you haven’t done so already, create an Android project.
- Right click on your project and select Build Path > Configure Build Path…
- Click the Add External Jars… button and choose the ActiveAndroid jar file.
First, clone the source from Git and install the package to your local repository:
git clone https://github.com/pardom/ActiveAndroid.gitcd ActiveAndroidmvn clean install
After the project builds successfully, add the following dependency to your pom.xml
<dependency>
<groupId>com.activeandroid</groupId>
<artifactId>activeandroid</artifactId>
<version>(insert latest version)</version>
</dependency>Now that you have ActiveAndroid added to you project, you can begin your two-step configuration process! The first thing we’ll need to do is add some global settings. ActiveAndroid will look for these in the AndroidManifest.xml file. Open the AndroidManifest.xml file located at the root directory of your project. Let’s add some configuration options.
- AA_DB_NAME (optional)
- AA_DB_VERSION (optional – defaults to 1)
The configuration strings for my project look like this
<manifest ...
<application android:name=".app.MyApplication" >
...
<meta-data android:name="AA_DB_NAME" android:value="Pickrand.db" />
<meta-data android:name="AA_DB_VERSION" android:value="5" />
</application>
</manifest>Notice also that the application name points to the ActiveAndroid application class. This step is required for ActiveAndroid to work. the name corresponds to your Application class extending the ActiveAndroid Application class. This is not required to extend it, however it's still required to override Application with AA's version in your AndroidManifest.xml to start AA properly .
If you are using a custom Application class, just extend com.activeandroid.app.Application instead of android.app.Application
public class MyApplication extends com.activeandroid.app.Application { ...But what if you're already doing this to utilize another library? Simply initialize and dispose ActiveAndroid in the Application class.
public class MyApplication extends SomeLibraryApplication {
@Override
public void onCreate() {
super.onCreate();
ActiveAndroid.initialize(this);
}
@Override
public void onTerminate() {
super.onTerminate();
ActiveAndroid.dispose();
}
}In our example two tables: Category and Item. In step two you will create classes for those tables. Don’t worry though, this part is easy too.
We'll go into more detail about setting up the database model later, but here are our classes. All name properties for annotations are optional in AA On Steroids
@Table(name = "Categories")//optional, the class name would also be sufficient
public class Category extends Model {
@Column(name = "Name")//optional, if no name is present, AA on Steroids takes the field name
@PrimaryKey // required for at least one field on the object-subclass-of-Model hierarchy
public String name;
//required to describe the object, however, plays no part in primary key creation
// should display the primary keys in order, separated by commas and encapsulated in parentheses
@Override
public String getId(){
return "(" + name + ")";
}
}
@Table
public class Item extends Model {
@Column @PrimaryKey
public String name;
@Column @PrimaryKey
public Category category;
@Override
public String getId(){
return "(" + name + "," + category + ")";
}
}That’s it for configuration. Next, let's learn about creating your database model.