ActiveAndroid ORM in android

In this article, I am focusing about the ActiveAdroid Orm database in Android. I have already discussed with GreenDAO fast ORM in android. If you still not checked then please check this link GreenDao fast ORM in android. Yes, this is really fast no doubt but this is required to used when we required a large amount of data to store into the database for offline and sync with online. I would be recommended to use this powerful ORM.

Now we need an SQLite database for a small application I would say to use the ActiveAndroid ORM.
This is pretty easy to add in any application and configuration is also very simple. Now the question is why ORM and this kind of wrapper if already having android SQLite database and SQL query? I would say now the time is an object oriented based concept. ActiveAndroid ORM is object relation mapping concept. It is very easy to understand and does not require any complex SQL query if you want any data from the table.

ActiveAndroid wrapper provides Object-Relational mapper for mapping the java classes to database tables and mapping java class member variables to the table columns. It does not require any raw query to get the information. It makes easy and pretty simple for developers because it is everything based on the object. Through the object, we can create and modify the table row.  For more detail Check Official DocumentActiveAndroid.

Before going through the code first check the uploaded video to understand What thing going to implement in this article.  Here is uploaded video.

Thanks, for watching the video.Ok, Let's create an android project for building the ActiveAndroid database. First of all, we need to required configuration.

1. Add the dependency into Gradle file.

build.gradle project level and app level

 repositories {
        jcenter()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    } 

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

2. Create Main Application and initialize the ActiveAndroid and add this application in the AndroidManfest file. Here you can add meta tag for database name and version. Even you can give configuration for dynamic also but Here I am setting in application tag only.

  
 public class MainApplication extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        ActiveAndroid.initialize(this);
    }
 }

And add this meta tag inside the application tag.
  
<meta-data android:name="AA_DB_NAME" android:value="AndroidPoint.db" />
<meta-data android:name="AA_DB_VERSION" android:value="1" />

Ok great, You have done so far. Let's create the Object model for a table.

Article.java

 
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;

/**
 * Created by sunil on 18-03-2017.
 */

@Table(name = "Article", id = "article_id")
public class Article extends Model{

    // This is the unique id given by the server
    @Column(name = "id")
    private long id;

    // This is a regular field
    @Column(name = "Title")
    public String title;

    @Column(name = "Description")
    public String description;

    @Column(name = "Author")
    public String author;

    // Make sure to have a default constructor for every ActiveAndroid model

    public Article(){
        super();
    }

    public Article (String title, String description, String author){
        super();
        this.title = title;
        this.description = description;
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

ArticleManager.java

import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;
import com.sunil.activeandroiddbapp.model.Article;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunil on 18-03-2017.
 */

public class ArticleManager {

    public static void addArticle(Article article){
        article.save();
    }

    public static List getAllArticle(){
List articles = new ArrayList<>();
articles = new Select().from(Article.class).execute(); return articles; } public static List getArticle(Article article){
List articles = new ArrayList<>();
articles = new Select().from(Article.class) .where("article_id = ?", article.getId()) .orderBy("Title ASC") .execute(); return articles; } public static void deleteArticle(Article article){ new Delete().from(Article.class).where("article_id = ?", article.getId()).execute(); } }

MainActivity.java

 
public class MainActivity extends AppCompatActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.addArticle)
    Button addArticle;

    private ArticleAdapter articleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        List list = getArticles();
if (list != null) { articleAdapter = new ArticleAdapter(this, list); recyclerView.setAdapter(articleAdapter); }else{ Toast.makeText(MainActivity.this, "No article found.", Toast.LENGTH_LONG).show(); } } private List getArticles(){
List articleList = ArticleManager.getAllArticle();
return articleList; } @OnClick(R.id.addArticle) public void addArticle(){ Intent intent = new Intent(MainActivity.this, AddNewArticleActivity.class); startActivity(intent); } }

AddNewArticleActivity.javap

 
/**
 * Created by sunil on 18-03-2017.
 */

public class AddNewArticleActivity extends AppCompatActivity {

    @BindView(R.id.articleName)
    EditText articleName;
    @BindView(R.id.articleDesc)
    EditText articleDesc;
    @BindView(R.id.addButton)
    Button addButton;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_acticle);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {

    }

    @OnClick(R.id.addButton)
    public void addNewArticle(){
        String articleTitle = articleName.getText().toString();
        String articleDescription = articleDesc.getText().toString();
        if (articleTitle.isEmpty() && articleDescription.isEmpty()){
            Toast.makeText(AddNewArticleActivity.this, "Please enter details.", Toast.LENGTH_LONG).show();
        }else{
            Article article = new Article();
            article.title= articleTitle;
            article.description =articleDescription;
            article.author ="Sunil";
            ArticleManager.addArticle(article);

            finish();
        }

    }
}


Wow looks pretty simple database implementation by using ActiveAndroid. I hope you like this article. Thanks for reading this post.

Comments

  1. I've been using it for a year but It is super outdated!

    ReplyDelete
    Replies
    1. Agree Akabar. But it helps to new developers to start fast build apps.

      Delete

Post a Comment

Popular posts from this blog

Service LifeCycle

NavigationView Drawer Expandable menu Item in ANdroid

Custom SeekBar (Discrete SeekBar) in android