アイテムのリストを表示するためにRecyclerviewがあり、Realmデータベースからデータを取り出しArrayListに格納しています。しかし、特定の位置の特定のBoxItemを更新しようとすると、このようなエラーが表示されます。通常のJavaオブジェクトとしてRealmObjectを継承するJavaクラスを使用できません
FATAL EXCEPTION: main Process: one.thebox.android, PID: 7236
java.lang.IllegalStateException: Changing Realm data can only be done from inside a transaction.
at io.realm.internal.Table.throwImmutable(Table.java:1138)
at io.realm.internal.Table.checkImmutable(Table.java:924)
at io.realm.internal.UncheckedRow.setLong(UncheckedRow.java:195)
at io.realm.BoxItemRealmProxy.realmSet$quantity(BoxItemRealmProxy.java:590)
at one.thebox.android.Models.BoxItem.setQuantity(BoxItem.java:347)
at one.thebox.android.adapter.SearchDetailAdapter$SearchedItemViewHolder.updateQuantityInCart(SearchDetailAdapter.java:870)
at one.thebox.android.adapter.SearchDetailAdapter$SearchedItemViewHolder.access$1900(SearchDetailAdapter.java:492)
at one.thebox.android.adapter.SearchDetailAdapter$SearchedItemViewHolder$7.onClick(SearchDetailAdapter.java:786)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
ここではいくつかのコードを見つけてください - ArrayList boxes;
boxItem.setQuantity(quantity);
CartHelper.updateQuantityInCart(boxItem, quantity);
boxItems.get(position).setQuantity(quantity);
notifyItemChanged(position);
ここでそれがエラーを示している、配列リスト内のオブジェクトを変更することができませんでし
BoxItem -
public class BoxItem extends RealmObject implements Serializable {
private int quantity;
@PrimaryKey
private String uuid;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
そしてCartHelper-は
データベース - レルムでの更新データクラスですpublic static void updateQuantityInCart(final BoxItem boxItem, final int quantity) {
Realm realm = TheBox.getRealm();
realm.beginTransaction();
BoxItem boxItem1 = realm.where(BoxItem.class).equalTo("uuid", boxItem.getUuid()).findFirst();
if (boxItem1 != null) {
boxItem1.setQuantity(quantity);
}
realm.commitTransaction();
}
updateQuantityInCart()
は、アダプタクラスのメソッドであり、私たちが起動したいときに呼び出されますrecyclerviewの任意の項目を日付にします。その特定のアイテムの数量を更新する場合のように
:あなたは、バックグラウンドスレッドで行う別の最善の方法は、可能性が
CartHelper.updateQuantityInCart(boxItem, quantity);
のようなすべての更新の間で
beginTransaction()
とcommitTransaction()
を追加する必要がありますトランザクション。 Realm 3.1.4以降を使用している場合は、 'RealmRecyclerViewAdapter'が' notifyItemChanged() '呼び出しを処理します。 – EpicPandaForce@EpicPandaForceので、RealmRecyclerViewAdapter'と私の現在のレルムバージョンを試してみてください。3.1.3 – rahul
@rahul boxItem/CartHelperとそのupdateQuantityInCartメソッドは明確ではありません。 – Stallion