我有3种不同的活动。 1扩展Application和realm在此活动中配置。 2.数据从第二个活动添加到Realm。 3.数据将显示在第3个活动中。我无法完成第3部分。我无法在第3个活动中获得Realm的实例。以下是申请书(我提到的第一项活动)
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(realmConfiguration);
}
以下是向第二个活动(PurchaseDetailsActivity)内部的片段中添加数据的代码。
private void addDataToRealm(DBPurchase model) {
mRealm.beginTransaction();
DBPurchase dbPurchaseModel = new DBPurchase();
dbPurchaseModel.setId(id);
dbPurchaseModel.setAmountPayed(model.getAmountPayed());
dbPurchaseModel.setCredit(model.getCredit());
mRealm.insertOrUpdate(dbPurchaseModel);
mRealm.commitTransaction();
id++;
最后一个是第3个活动中的片段。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_purchase_items, container, false);
purchaseTotalListAdapter = new PurchaseTotalListAdapter(getContext(), mDBPurchaseArrayList, PurchasesTotalListFragment.this);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(purchaseTotalListAdapter);
Realm.getDefaultInstance();
return view;
}