领域更新一个对象,其他实例也更新

时间:2017-10-11 15:05:18

标签: java android realm

我有课程订单:

public class Order extends RealmObject {
@PrimaryKey
@SerializedName("Id")
private int id;
@SerializedName("Contractor")
private Contractor contractor;
@SerializedName("ItemAmounts")
private RealmList<ItemAmount> itemsAmounts = new RealmList<>();
@SerializedName("ModificationDate")
private Date modificationDate;
@SerializedName("ExportDate")
private Date exportDate;
@SerializedName("Export")
private boolean exportStatus;
@SerializedName("User")
private User user;

public int getId() {
    return id;
}

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

public Contractor getContractor() {
    return contractor;
}

public void setContractor(Contractor contractor) {
    this.contractor = contractor;
}

public RealmList<ItemAmount> getItemsAmounts() {
    return itemsAmounts;
}


public void setItemsAmounts(RealmList<ItemAmount> itemsAmounts) {
    this.itemsAmounts = itemsAmounts;
}

public Date getModificationDate() {
    return modificationDate;
}

public void setModificationDate(Date modificationDate) {
    this.modificationDate = modificationDate;
}

public Date getExportDate() {
    return exportDate;
}

public void setExportDate(Date exportDate) {
    this.exportDate = exportDate;
}

public boolean isExportStatus() {
    return exportStatus;
}

public void setExportStatus(boolean exportStatus) {
    this.exportStatus = exportStatus;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

} 我创建任何新订单保存到Realm:

  public void insertOrUpdate(Order order) {
    Order orderToCompare = realm.where(Order.class)
            .equalTo("id", order.getId()).findFirst();


    realm.beginTransaction();
    if (orderToCompare == null) {
        Order orderRealm = realm.createObject(Order.class, order.getId());
        orderRealm.setExportStatus(order.isExportStatus());
        orderRealm.setContractor(order.getContractor());
        orderRealm.setModificationDate(order.getModificationDate());
    } else if (!orderToCompare.equals(order)) {
        realm.copyToRealmOrUpdate(order);
    }

    realm.commitTransaction();


}

我的所有订单都有ItemAmount列表,而ItemAmount有单项(项目也包含在Realm中)和他的金额。因此,当我尝试在一个订单中更改项目数量时,其他ItemAmount中的所有金额都会更新。在我更新金额的功能之下:

     recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {
            itemPosition = position;
            AlertDialog.Builder builder = new AlertDialog.Builder(OrderItemsActivity.this);
            View mView = getLayoutInflater().inflate(R.layout.change_amount, null);
            builder.setView(mView);
            final AlertDialog alertDialog = builder.create();
            alertDialog.show();
            Button changeAmountButton = (Button) mView.findViewById(R.id.changeAmountButton);
            final EditText setAmountEditText = (EditText) mView.findViewById(R.id.setAmountEditText);

            changeAmountButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (setAmountEditText.getText().toString().isEmpty()) {
                        Toast.makeText(getApplicationContext(), "Pole nie może być puste", Toast.LENGTH_SHORT).show();
                    } else {
                        orderDAO = new OrderDAO();
                        orderDAO.updateAmountOfItem(Integer.parseInt(setAmountEditText.getText().toString()), itemPosition, selectedOrderId);
                        itemAmounts.clear();
                        alertDialog.dismiss();
                        prepareOrderData();

                    }

                }
            });
        }

 public void updateAmountOfItem(int amount, int itemPosition, int orderId) {
    realm.beginTransaction();
    Order orderToCompare = realm.where(Order.class)
            .equalTo("id", orderId).findFirst();
    orderToCompare = realm.copyFromRealm(orderToCompare);
    RealmList<ItemAmount> itemAmounts = orderToCompare.getItemsAmounts();
    ItemAmount itemAmount = itemAmounts.get(itemPosition);
    itemAmount.setAmount(amount);
    itemAmounts.set(itemPosition,itemAmount);
    orderToCompare.setItemsAmounts(itemAmounts);
    realm.insertOrUpdate(orderToCompare);
    realm.commitTransaction();
}

如何解决?因为我只需要更新一个实例,而不是所有实例。

0 个答案:

没有答案