我有包含 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 中删除它。我该如何实施?