0

我有包含 GroupItem 的 ForeignCollection 的 ContactItem:

public class ContactItem implements Parcelable{
    @DatabaseField(generatedId = true)
    private Integer id;
    @DatabaseField(dataType = DataType.STRING, columnName = Constants.CONTACT_NAME)
    private String name;
    @DatabaseField(dataType = DataType.STRING, columnName = Constants.CONTACT_NUMBER)
    private String number;
    @DatabaseField(dataType = DataType.INTEGER, columnName = Constants.CONTACT_ICON)
    private int icon;
    @DatabaseField(dataType = DataType.INTEGER, columnName = Constants.CONTACT_DAYS)
    private int days;
    @DatabaseField(foreign=true,canBeNull = true,foreignAutoRefresh = true,columnName = Constants.CONTACT_GROUP)
    private GroupItem group;
    @ForeignCollectionField(columnName = Constants.CONTACT_GROUP_LIST)
    private ForeignCollection<GroupItem> groups;
}

和 GroupItem,其中包含 ContactItem 的 ForeignCollection:

public class GroupItem implements  Parcelable{
    @DatabaseField(generatedId = true)
    private Integer id;
    @DatabaseField(dataType = DataType.STRING, columnName = Constants.GROUP_NAME)
    private String name;
    @DatabaseField(dataType = DataType.INTEGER, columnName = Constants.GROUP_ICON)
    private int icon;
    @DatabaseField(dataType = DataType.INTEGER, columnName = Constants.GROUP_COUNT)
    private int count;
    @DatabaseField(foreign=true,canBeNull = true,foreignAutoRefresh = true, columnName = Constants.GROUP_CONTACT)
    private ContactItem contact;
    @ForeignCollectionField(eager = true, columnName=Constants.GROUP_CONTACTS_LIST)
    private ForeignCollection<ContactItem> contacts;
    @DatabaseField(dataType = DataType.INTEGER, columnName = Constants.GROUP_DAYS)
    private int days;
}

我需要从 GroupItem 的 ContactItems 的 ForeignCollection 中删除 ContactItem。我这样做:

  public void removeContactFromGroup(GroupItem group, ContactItem contact)
        {
            DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
            Dao<GroupItem,Integer> daoGroup = null;
            Dao<ContactItem,Integer> daoContact = null;
            GroupItem newGroup = null;
             try {
                 daoGroup = db.getGroupDao();
                 daoContact = db.getContactDao();
                 UpdateBuilder<GroupItem, Integer> updateBuilder = daoGroup.updateBuilder();
                 newGroup = daoGroup.queryForId(group.getId());
                 if ( newGroup.getContactCollection().contains(contact))
                 {


                 }
             }
             catch(Exception e)
             {
                 Log.i(" saveGroupContacts database problem","It is cause problem");
                 e.printStackTrace();
             }
        }

但它会从整个数据库中删除 ContactItem。但我只需要从 ForeignCollection 中删除它。我该如何实施?

4

1 回答 1

0

我通过可能不优雅但可行的解决方案解决了我的问题:我只是将我的嵌套对象设置在其中,其中包含它为 null 并使用 dao 更新对象容器:

public void removeContactFromGroup(GroupItem group, ContactItem contact)
        {
            DatabaseHandler db = new DatabaseHandler(context.getApplicationContext());
            Dao<GroupItem,Integer> daoGroup = null;
            Dao<ContactItem,Integer> daoContact = null;
            GroupItem newGroup = null;
            ContactItem contactFromDb = null;
             try {
                 daoGroup = db.getGroupDao();
                 daoContact = db.getContactDao();
                 UpdateBuilder<GroupItem, Integer> updateBuilder = daoGroup.updateBuilder();
                 newGroup = daoGroup.queryForId(group.getId());
                 contactFromDb = daoContact.queryForId(contact.getId());
                 contactFromDb.setContactGroup(null);
                 daoContact.update(contactFromDb);

             }
             catch(Exception e)
             {
                 Log.i(" saveGroupContacts database problem","It is cause problem");
                 e.printStackTrace();
             }
        }

为我工作。

于 2013-09-05T08:53:05.067 回答