Skip to content

Instantly share code, notes, and snippets.

@RobertoArtiles
Last active November 25, 2015 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertoArtiles/cd729407145144ad5da6 to your computer and use it in GitHub Desktop.
Save RobertoArtiles/cd729407145144ad5da6 to your computer and use it in GitHub Desktop.
A temporary solution to use RecyclerView with Realm (based on RealmBaseAdapter). Note: make sure you create an io.realm package, and put this class there.
package io.realm;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
public abstract class RealmBaseRecyclerViewAdapter<T extends RealmObject, M extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<M> {
protected LayoutInflater inflater;
protected RealmResults<T> realmResults;
protected Context context;
private RealmChangeListener listener;
public RealmBaseRecyclerViewAdapter(Context context, RealmResults<T> realmResults, boolean automaticUpdate) {
if (context == null) {
throw new IllegalArgumentException("Context cannot be null");
}
this.context = context;
this.realmResults = realmResults;
this.inflater = LayoutInflater.from(context);
this.listener = (!automaticUpdate) ? null : new RealmChangeListener() {
@Override
public void onChange() {
notifyDataSetChanged();
}
};
if (listener != null && realmResults != null) {
realmResults.getRealm().addChangeListener(listener);
}
}
@Override
public int getItemCount() {
if (realmResults == null) {
return 0;
}
return realmResults.size();
}
public T getItem(int i) {
if (realmResults == null) {
return null;
}
return realmResults.get(i);
}
/**
* Returns the current ID for an item. Note that item IDs are not stable so you cannot rely on
* the item ID being the same after {@link #notifyDataSetChanged()} or
* {@link #updateRealmResults(RealmResults)} has been called.
*
* @param i Index of item in the adapter
* @return Current item ID.
*/
@Override
public long getItemId(int i) {
// TODO: find better solution once we have unique IDs
return i;
}
/**
* Update the RealmResults associated to the Adapter. Useful when the query has been changed.
* If the query does not change you might consider using the automaticUpdate feature
*
* @param queryResults the new RealmResults coming from the new query.
*/
public void updateRealmResults(RealmResults<T> queryResults) {
if (listener != null) {
// Making sure that Adapter is refreshed correctly if new RealmResults come from another Realm
if (this.realmResults != null) {
this.realmResults.getRealm().removeChangeListener(listener);
}
if (queryResults != null) {
queryResults.getRealm().addChangeListener(listener);
}
}
this.realmResults = queryResults;
notifyDataSetChanged();
}
}
@saket
Copy link

saket commented Sep 8, 2015

Note: make sure you put it in io.realm package.

I'm sorry if this is a noob question, but how do I put this file in the io.realm package? Changing package isn't possible because the corresponding file path needs to be the same too. What am I missing?

Update: I simply created a new package "io.realm". Is this what you meant?

@RobertoArtiles
Copy link
Author

@Saketme Sorry, completely missed your message. If it's still relevant, what you did is exactly what I meant 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment