我有VisitingCardPOJO
表单。我想要更新单个条目说明否= 3
VisitingCardPOJO.java
public class VisitingCardPOJO extends RealmObject {
@PrimaryKey private int no;
private String name;
private String address;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
答案 0 :(得分:25)
我找到了解决方案,并且工作正常
public void updateNewCard(Realm realm, VisitingCardPOJO card) {
realm.beginTransaction();
VisitingCardPOJO toEdit = realm.where(VisitingCardPOJO.class)
.equalTo("no", card.getNo()).findFirst();
toEdit.setName(card.getName());
toEdit.setAddress(card.getAddress());
realm.commitTransaction();
}
答案 1 :(得分:6)
实际的正确方法是
public void updateNewCard(Realm realm, VisitingCardPOJO card) {
final int no = card.getNo();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
VisitingCardPOJO toEdit = realm.where(VisitingCardPOJO.class)
.equalTo("no", no).findFirst();
if(toEdit != null) {
toEdit.setName(card.getName());
toEdit.setAddress(card.getAddress());
}
}
});
}