Using Realm.io to store money values

前端 未结 4 1460
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 09:11

I\'m starting to play with Realm.io in an Android app that I\'m writing. In one of my data objects, I\'m required to store a currency value. Previously I had stored the valu

4条回答
  •  梦毁少年i
    2021-01-18 09:52

    My solution:

    Define Interface:

    public interface RealmConvert {
       void convertToDB();
    
       void convertToObj();
    }
    

    Define Entity:

    @Ignore
    private BigDecimal balance;
    private String balanceStr;
    
    @Override public void convertToDB() {
      if (getBalance() != null) {
        setBalanceStr(getBalance().toString());
      }
    }
    
    @Override public void convertToObj() {
      if (getBalanceStr() != null) {
        setBalance(new BigDecimal(getBalanceStr()));
      }
    }
    

    Before you copyToRealm:call method convertToDB

    When you need to use the entity: call method convert obj

    It's not an elegant solution, but it works.

    Christian Melchior's answer doesn't work in my app.

提交回复
热议问题