Room:如何获取会员的实际余额,并将其用于setter方法来设置特定会员的余额

Room: How to get actual balance of a member and use it for setter method to set balance of the specific member

我使用 Android Room 作为我的数据库。我有一个标题屏幕,当我点击那里的按钮时,我的 activity 打开。 activity 显示了保存在数据库中的成员列表。我的实体成员由名字、姓氏和余额组成。我可以添加新成员。用户可以设置名字和姓氏,余额自动设置为 0。我有另一个实体称为交易,我可以在其中添加交易和 link 与成员的交易。

如果我为会员a添加两笔新交易,会员a的余额就会增加。我可以在我的会员activity中点击一个会员,然后我会看到该会员的附加信息。在那里,我可以看到他在我的 DAO 中使用 SUM() 查询的实际余额。

但我不知道如何使用该查询来设置会员的余额。我也不知道应该在哪里(在哪个class)使用成员class.

的set Balance方法

有人可以帮我解决这个问题吗?如果您需要任何特定代码,只需在评论中写下,我会将其添加到这个问题中。谢谢!

实体成员:

@TypeConverters(Converters.class)
@Entity(tableName = "member_table")
public class Member {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "MemberID")
    private long id;

    @ColumnInfo(name = "FirstName")
    private String firstname;

    @ColumnInfo(name = "Surname")
    private String surname;

    @ColumnInfo(name = "MemberBalance")
    private BigDecimal balance;

    private boolean isSelected = false;

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }


    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Member(String firstname, String surname) {
        this.firstname = firstname;
        this.surname = surname;
        this.balance = new BigDecimal(0).setScale(2, RoundingMode.HALF_EVEN);
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    public boolean isSelected() {
        return isSelected;
    }

}

实体交易:

@TypeConverters(Converters.class)
@Entity(tableName = "transaction_table")
public class Transaction {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "TransactionID")
    private long id;

    @ColumnInfo(name = "TransactionName")
    private String transactionName;

    @ColumnInfo(name = "TransactionBalance")
    private BigDecimal balance;

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTransactionName() {
        return transactionName;
    }

    public void setTransactionName(String transactionName) {
        this.transactionName = transactionName;
    }

    public Transaction(String transactionName, BigDecimal balance) {
        this.transactionName = transactionName;
        this.balance = new BigDecimal(String.valueOf(balance)).setScale(2, RoundingMode.HALF_EVEN);
    }
}

第一张图片:这是我的会员activity,在这里我可以看到所有会员的名单。余额一直是0.00,因为我从来没有用过setBalance()方法,余额应该是getBalance()方法得到的值。

第二张图片:这是我的交易activity,我可以在其中看到所有交易的列表。

第三张图:这个activity是我点击一个会员开始的。然后我看到会员名称和他参与的所有交易。这里会员的余额是用一个DAO-Query取的:SELECT SUM(TransactionBalance)FROM member_transaction_table INNER JOIN transaction_table ON transaction_table.TransactionID = member_transaction_table.Transaction_ID WHERE member_transaction_table.Member_ID =:memberid

我不知道如何以及在何处让应用程序计算交易。计算返回的值应使用 setBalance() 方法设置。

我建议根本不要在 Member class 中使用列 balance,因为看起来所有数据都应该存储在这个 transaction 实体中。如果您必须显示特定 Member 的余额,您应该每次都计算他的余额。这种方法有一个主要缺点,即从性能的角度来看并不有效。如果性能比拥有 balance 列更重要,那是个好主意,但每次向系统添加新 transaction 时都必须更新 Member 实体。

通过在添加交易时设置余额解决了我的问题。所以我把交易的价值简单地加到我的会员余额中。