Update statement in Realm

前端 未结 2 1730
醉话见心
醉话见心 2021-02-14 05:38

I have VisitingCardPOJO form table. I want update single entry say where no=3

VisitingCardPOJO.java

public class VisitingC         


        
相关标签:
2条回答
  • 2021-02-14 06:25

    I found the solution as and It worked

     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();
        }
    

    see demo for Realm

    0 讨论(0)
  • 2021-02-14 06:27

    The actual right way to do it is this

    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());
                }
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题