Realm-java: RealmRecyclerViewAdapter: Replace OrderedRealmCollection by java.util.List

Created on 1 Nov 2017  路  4Comments  路  Source: realm/realm-java

I have fragment with RecyclerView.

I need the next:

  1. When Realm was changed in fragment the RecyclerVeiw must autoupdate.
  2. Data show in sorted order

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

T-Help

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

harshvishu picture harshvishu  路  3Comments

tloshi picture tloshi  路  3Comments

jjorian picture jjorian  路  3Comments

David-Kuper picture David-Kuper  路  3Comments

wyvern610 picture wyvern610  路  3Comments