I have fragment with RecyclerView.
I need the next:
Here my fragment:
public class MyFragment extends Fragment {
private RealmRecyclerViewAdapter sortAdapter;
private Realm realm;
private RealmResults<?> realmResults;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
realm = Realm.getDefaultInstance();
realmResults = realm.where(Offer.class).findAllSorted(sortByFieldName, Sort.DESCENDING);
sortAdapter = new OfferSortAdapter(realmResults)
sortRecyclerView.setAdapter(sortAdapter);
}
Here my adapter:
public class OfferSortAdapter extends RealmRecyclerViewAdapter<Offer, OfferSortAdapter.OfferViewHolder> {
public OfferSortAdapter(OrderedRealmCollection<Offer> data) {
super(data, true);
setHasStableIds(true);
}
And as result all work fine. When Realm change MyFragmentis autoupdate. Nice!
As you can see in constructor OfferSortAdapterI use parameter OrderedRealmCollection.
Question:
Can I use java.util.Listas parameter in OfferSortAdapter? Is it does not break anything?
Something like this:
myFragment:
public class MyFragment extends Fragment {
private RealmRecyclerViewAdapter sortAdapter;
private Realm realm;
private RealmList<Offer> offers = new RealmList<>();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
realm = Realm.getDefaultInstance();
realmList= MyService.getOffersAsRealmList(int id)
sortAdapter = new OfferSortAdapter(realmList)
sortRecyclerView.setAdapter(sortAdapter);
}
adapter:
public OfferSortAdapter(java.util.List<Offer> data) {
super((OrderedRealmCollection<Offer>) data, true);
setHasStableIds(true);
}
I remind you that I need 2 things:
1.When Realm was changed in fragment the RecyclerVeiwmust autoupdate.
2.Data show in sorted order
private RealmList<Offer> offers = new RealmList<>();
This will not work with RealmRecyclerViewAdapter because this is an unmanaged collection.
You should use RealmResults for this, unless the RealmList is obtained from a managed RealmObject.
So I can obtain RealmListonly from RealmObject. But RealmObjectI can obtain if only create query to Realm?
Something like this:
Organization findOrganization = realm.where(Organization.class)
.equalTo(Organization.ID, organizationId)
.findFirst();
RealmList<Offer> offersRealmList = findOrganization.getOffers();
Yup
@lmdic The above seems to answer you question, so I'm closing this issue. Feel free to reopen if that is not the case