下面给出的是一个片段(PurchaseFinalDetailsFragment),它从另外两个片段(PurchaseItemsFragment和PurchaseAddPayedEmployeeFragment)获取值。我有3个模型类。 1.整个购买,2。项目详情,3。对于付款的员工详细信息。 2和3也是模型类1中的成员。我收到了illegalArgumentException。在下面发布代码和日志。任何帮助都非常有用。 TIA
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmResults;
public class PurchaseFinalDetailsFragment extends BaseFragment implements PurchaseAddPayedEmployeeFragment.OnFragmentInteraction {
private static final String ARG_PURCHASE = "purchase";
private static RealmList<DBPurchase> purchaseRealmList = new RealmList<>();
private static RealmList<DBPurchaseQtyItem> purchaseQtyItemRealmList = new RealmList<>();
private static RealmList<DBPurchaseEmployee> purchaseEmployeeRealmList = new RealmList<>();
private static int id = 1;
DBPurchase mDBPurchase = new DBPurchase();
DBPurchaseQtyItem mDBPurchaseQtyItem = new DBPurchaseQtyItem();
DBPurchaseEmployee mDBPurchaseEmployee = new DBPurchaseEmployee();
DBPurchaseItem mDBPurchaseItem = new DBPurchaseItem();
DBEmployee mDBEmployee = new DBEmployee();
private Realm mRealm;
private Purchase mPurchase;
private ArrayList<PayedEmployee> mPayedEmployees = new ArrayList<>();
public PurchaseFinalDetailsFragment() {
// Required empty public constructor
}
public static PurchaseFinalDetailsFragment newInstance(Purchase purchase) {
PurchaseFinalDetailsFragment fragment = new PurchaseFinalDetailsFragment();
Bundle args = new Bundle();
args.putString(ARG_PURCHASE, NavigationUtils.getStringForObject(purchase));
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
mPurchase = NavigationUtils.getObjectFromString(bundle.getString(ARG_PURCHASE), Purchase.class);
}
mRealm =Realm.getDefaultInstance();
}
@Override
public void onDestroy() {
super.onDestroy();
if(mRealm != null) { mRealm.close(); }
}
public void initView(View rootView) {
mEmployeeAdpater = new EmployeeAdpater(getContext(), mPayedEmployees,PurchaseFinalDetailsFragment.this);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFinalDetails();
}
});
}
public void getFinalDetails() {
mDBPurchase.setAmountPayed(Double.parseDouble(amountPaidEditText.getText().toString()));
mDBPurchase.setTotalAmount(Double.parseDouble(totalAmountEditText.getText().toString()));
mDBPurchase.setCredit(Double.parseDouble(creditEditText.getText().toString()));
for (int i = 0; i < mPayedEmployees.size(); i++) {
mDBEmployee.setName(mPayedEmployees.get(i).getName());
mDBPurchaseEmployee.setEmployee(mDBEmployee);
mDBPurchaseEmployee.setAmount(mPayedEmployees.get(i).getAmountPaid());
}
purchaseEmployeeRealmList.add(mDBPurchaseEmployee);
mDBPurchase.setPayedEmployees(purchaseEmployeeRealmList);
for (int i = 0; i < mPurchase.getPurchaseItems().size(); i++) {
mDBPurchaseItem.setName(mPurchase.getPurchaseItems().get(i).getName());
mDBPurchaseQtyItem.setItem(mDBPurchaseItem);
mDBPurchaseQtyItem.setQuantity(mPurchase.getPurchaseItems().get(i).getQuantity());
mDBPurchaseQtyItem.setAmount(mPurchase.getPurchaseItems().get(i).getAmount());
}
purchaseQtyItemRealmList.add(mDBPurchaseQtyItem);
mDBPurchase.setItems(purchaseQtyItemRealmList);
mDBPurchase.setCreatedAt(parseDate(createdDate));
mDBPurchase.setLastUpdatedAt(parseDate(Integer.toString(calender.get(Calendar.DATE))));
addDataToRealm(mDBPurchase);
}
}
private void addDataToRealm(DBPurchase model) {
mRealm.beginTransaction();
DBPurchase dbPurchaseModel = mRealm.createObject(DBPurchase.class);
dbPurchaseModel.setId(id);
dbPurchaseModel.setAmountPayed(model.getAmountPayed());
dbPurchaseModel.setCredit(model.getCredit());
dbPurchaseModel.setTotalAmount(model.getTotalAmount());
dbPurchaseModel.setItems(purchaseQtyItemRealmList);
dbPurchaseModel.setPayedEmployees(purchaseEmployeeRealmList);
dbPurchaseModel.setCreatedAt(model.getCreatedAt());
dbPurchaseModel.setLastUpdatedAt(model.getLastUpdatedAt());
purchaseRealmList.add(dbPurchaseModel);
mRealm.commitTransaction();
//purchaseFinalDetailsAdapter.notifyDataSetChanged();
id++;
}
}
模型类是:
public class DBPurchase extends RealmObject {
private int id;
private RealmList<DBPurchaseQtyItem> items;
private double totalAmount;
private double amountPayed;
private double credit;
@Nullable
private RealmList<DBPurchaseEmployee> payedEmployees;
private Date createdAt = new Date();
private Date lastUpdatedAt;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public RealmList<DBPurchaseQtyItem> getItems() {
return items;
}
public void setItems(RealmList<DBPurchaseQtyItem> items) {
this.items = items;
}
public double getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(double totalAmount) {
this.totalAmount = totalAmount;
}
public double getAmountPayed() {
return amountPayed;
}
public void setAmountPayed(double amountPayed) {
this.amountPayed = amountPayed;
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
public RealmList<DBPurchaseEmployee> getPayedEmployees() {
return payedEmployees;
}
public void setPayedEmployees(RealmList<DBPurchaseEmployee> payedEmployees) {
this.payedEmployees = payedEmployees;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public Date getLastUpdatedAt() {
return lastUpdatedAt;
}
public void setLastUpdatedAt(Date lastUpdatedAt) {
this.lastUpdatedAt = lastUpdatedAt;
}
}
DBPurchaseQtyItem和DBPurchase Employee本身是扩展realmObject的模型类。我收到illegalArgumentException。发布以下日志:
java.lang.IllegalArgumentException: Each element of 'value' must be a valid managed object.
at io.realm.DBPurchaseRealmProxy.realmSet$items(DBPurchaseRealmProxy.java:184)
at app.model.db.DBPurchase.setItems(DBPurchase.java:47)
at app.ui.PurchaseFinalDetailsFragment.addDataToRealm(PurchaseFinalDetailsFragment.java:263)
at app.ui.PurchaseFinalDetailsFragment.getFinalDetails(PurchaseFinalDetailsFragment.java:205)
at app.ui.PurchaseFinalDetailsFragment$4.onClick(PurchaseFinalDetailsFragment.java:168)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:0)
根据例外,
dbPurchaseModel.setItems(purchaseQtyItemRealmList); // <-- this line
无法将非托管RealmList设置为托管对象的字段。
所以而不是
DBPurchase dbPurchaseModel = mRealm.createObject(DBPurchase.class);
应该是
DBPurchase dbPurchaseModel = new DBPurchase();
和
mRealm.insertOrUpdate(dbPurchaseModel); // copyToRealmOrUpdate() before 1.1.1
mRealm.commitTransaction();
虽然我对static List<...>
(以及特别是非托管RealmLists)持谨慎态度,但它通常是错误设计选择的指标。
答案 1 :(得分:0)
https://github.com/nateshmbhat/FresherAssignment2020/tree/nateshmbhat/Apps/CounterApp_MVP_Dagger/app/src/main/java/com/techy/nateshmbhat/contacto帮助了我。我只是发布我的工作代码段。
当心:在执行重新分配操作后,被替换的旧对象仍然留在数据库中。您可能希望更改现有列表,而不是重新分配。
kotlin:
// replacing a list
val newList = listContents.map { RealmString(it) }
db.db.executeTransaction { database ->
queriedList.realmList = RealmList(*database.copyToRealm(newList).toTypedArray())
}
// replacing a list element
val newElement = RealmString("wow")
db.db.executeTransaction { database ->
queriedList.realmList[0] = database.copyToRealm(newElement)
}