V
V
Valera Kuznetsov2014-10-27 22:36:33
Android
Valera Kuznetsov, 2014-10-27 22:36:33

How to implement 1 : M relation in ActiveAndroid library?

I use the ActiveAndroid library as a local database.
there is a Post model

package smartfoxlabs.ppos.Models;

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

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

/**
 * Created by dwite_000 on 24.10.2014.
 */
@Table(name = "posts")
public class Post extends Model {

    @Column(name = "postID",unique = true)
    public int id;

    @Column(name = "type")
    public String type;

    //WTF?
    @Column(name = "slug")
    public String slug;

    @Column(name = "url")
    public String url;

    @Column(name = "status")
    public String status;

    @Column(name = "title")
    public String title;

    @Column(name = "title_plain")
    public String title_plain;

    @Column(name = "content")
    public String content;

    @Column(name = "excerpt")
    public String excerpt;

    @Column(name = "PostDate")
    public String date;

    @Column(name = "Category")
    public java.util.List<Category> categories;

    @SuppressWarnings("serial")
    public static class List extends ArrayList<Post> {

    }

    public boolean exists() {
        boolean flag = false;
        //Post post = new Select().from(Post.class).where("Min = ?",this.id).executeSingle();
        return flag;
    }

    public Post() {
        super();
    }
}

there is a Categories model
@Table(name = "Categories")
public class Category extends Model {

    @Column(name = "Mid")
    public int id;

    @Column(name = "slug")
    public String slug;

    @Column(name = "title")
    public String title;

    @Column(name = "parent")
    public int parent;

    public Category() {
        super();
    }
}

A Post can have several categories, so when I call save, for some reason it does not save the list of categories for Post.
UPD: as I understand it, I still have an M : M Relationship, since a Post can have many Categories, and a Category has many Posts, so you need to do something like this . But I didn’t understand, is it necessary to create this object for each Post-Category pair separately and save it instead of the Post Category? or how?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question