当我想更新一个对象而我无法找到解决方案时,我遇到了一些RealClists的麻烦。 我知道有一个类似的主题,但它没有答案。
以下是我的两个课程:
public class Category extends RealmObject {
@PrimaryKey
private long id;
private String label;
private boolean isIncome;
private double defaultBudget;
private boolean isArchived;
private RealmList<Amount> amounts;
private RealmList<AutomaticTransaction> automaticTransactions;
}
public class Amount extends RealmObject {
@PrimaryKey
private long id;
private Category category;
private int day;
private Month month;
private String label;
private double amount;
}
我通过创建具有旧属性的新类别来更新类别,然后设置新值
Category updatedCategory = category.getUpdatedCategory();
updatedCategory.setLabel(labelEditText.getText().toString());
updatedCategory.setDefaultBudget(budget);
databaseHandler.addOrUpdateCategory(updatedCategory);
public Category getUpdatedCategory(){
return(new Category(id, label, isIncome, defaultBudget, amounts,
automaticTransactions));
}
在交易之前,列表已满,在它清空后,我不明白为什么。
public void addOrUpdateCategory(final Category category){
Log.e("test",category.getAmounts().size() + "");
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.copyToRealmOrUpdate(category);
Log.d("DatabaseHandler","add or update : " + category);
}
});
Log.e("test2",category.getAmounts().size() + "");
}
E/test: 31
D/DatabaseHandler: add or update : Category id : 7 - label : Fast food
& restaurants - income : false - defaultBudget : 50.0 - archived :
false - amounts : 0 - automaticTransactions : 0
E/test2: 0
感谢您的帮助,我真的被困住了。