【问题标题】:How make relationship between two entities - Sugar ORM如何建立两个实体之间的关系 - Sugar ORM
【发布时间】:2016-07-21 16:46:55
【问题描述】:

我在我的 Android 应用中使用 Sugar ORM,我想知道如何在实体之间建立 关系。我想要的是拥有更多的电子邮件和电话号码(工作、个人、家庭)

我试过这个:

我可以这样添加..

Contact contact = new Contact();
ContactItem contactItem = new ContactItem();
contactItem.setType(1);
contact.getItems.add(contactItem);

但是当我想加载时,我有一个错误。 无法从 Sqlite3 数据库中读取类。

联系方式

public class Contact extends SugarRecord {

    private String name;
    private String phoneNumber;
    private String email;
    private boolean favourite;
    private String imagePath;
    @Ignore private boolean visibleFirstLetter;

    public List<ContactItem> items;

    public Contact() {
        //empty constructor
    }

    public Contact(String name, String number, String email, boolean favourite, String imagePath) {
        this.name = name;
        this.phoneNumber = number;
        this.email = email;
        this.favourite = favourite;
        this.imagePath = imagePath;
        this.items = new ArrayList<>();
    }

    public void setName(String name) {this.name = name; }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setFavourite(Boolean favourite) {
        this.favourite = favourite;
    }

    public void setVisibleFirstLetter(Boolean visibleFirstLetter) {
        this.visibleFirstLetter = visibleFirstLetter;
    }
    public String getName() {
        return name;
    }

    public String getEmail() { return email; }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public boolean isFavourite() { return favourite; }

    public boolean isVisibleFirstLetter() {
        return visibleFirstLetter;
    }

    public String getImagePath() { return imagePath; }

    public void setImagePath(String imagePath) { this.imagePath = imagePath; }

    public List<ContactItem> getItems() {
        return items;
    }
}

物品

public class ContactItem {

    String content;
    Integer type;
    Contact contact;


    public static final Integer HOME = 0;
    public static final Integer WORK = 1;
    public static final Integer EMAIL = 2;
    public static final Integer FAX = 3;

    public ContactItem() {
        //empty constructor
    }
    public ContactItem(Integer type, String content) {
        this.type = type;
        this.content = content;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() { return content; }

    public Integer getType() { return type; }

    public Contact getContact() { return contact; }

    public void setContact(Contact contact) { this.contact = contact; }
}

【问题讨论】:

  • Contact 没有像 id 这样的字段。试过这个:Contact contact = SugarRecord.findById(Contact.class, 1);,记录索引从索引 1 开始。
  • 我试过了,还是不行。 java.lang.NullPointerException - “com.seznam_kontaktu.seznamkontaktu.Model.Contact.getName()' 在空对象引用上”
  • 我更新了我的问题。请检查一下。

标签: android database orm android-recyclerview


【解决方案1】:

一般来说,你应该在查找或更新之前初始化Contact。 可以是Contact 有方法getName() 为空。

在这种情况下,Model.Contact.getName()是一个空对象引用。

所以,你应该检查我的代码:

Contact contact = new Contact("Stepan", "09293293", stepan.stack@gmail.com, true);
contact.save();
Contact contact = SugarRecord.findById(Contact.class, (long) 1);

您应该尝试使用代码来加载所有联系人,这看起来像 findById,但它会在 Contact 中获取所有行:

List&lt;Contact&gt; contacts = SugarRecord.listAll(Contact.class);

在调试器 Android Studio 中,请查看所有值。您的数据可能不正确。

【讨论】:

  • 我想要的是将(名称、号码等.....)保存到数据库,在回收站视图中呈现名称(我有这两个东西)。但是,如果我在回收站视图中单击一个项目,它将打开一个对话框,其中包含数据库中的联系人。但是,我现在卡住了
  • 你保存到数据库成功了吗? getName 为空。我认为这可能有问题,请检查数据库中的字段name 是否为空?并查看我的更新答案。
  • 是的,回收站视图中的名称显示正确。所以它应该是空的
【解决方案2】:

您将需要所谓的链接器类,因为 Sugar 没有一对一、一对多或多对多关系的概念(列表和对象不能存储在 Sugar 中)。只有字符串、整数等:

    public class ContactItem extends SugarRecord {

        @Column(name = "contact_linker_id", unique = true)
        @Expose
        private int mContactId;

         String content;
         Integer type;
         Contact contact;

        }

当你第一次创建对象时:

Contact contact = new Contact();
contact.getId() //or other unique identifier
ContactItem contactItem = new ContactItem();
contactItem.mContactId = contact.getId();
contactItem.save();
contact.save();

当您从 DB 获取时,您应该获取所需的所有联系人对象并以这种方式获取它的对象:

Contact contact = Select.from(Contact.class).first();
ContactItem contactItem = Select.from(ContactItem.class).where("contact_linker_id").eq(contact.getId());
contact.addItem(contactItem);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多