Skip to content

Instantly share code, notes, and snippets.

@xingrz
Last active May 22, 2020 19:46
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xingrz/95e4aa31e386b3629bc5 to your computer and use it in GitHub Desktop.
Save xingrz/95e4aa31e386b3629bc5 to your computer and use it in GitHub Desktop.
A RecyclerView.Adapter-like Adapter that binds RealmResults to ListView
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import io.realm.RealmObject;
import io.realm.RealmResults;
public abstract class RealmAdapter<E extends RealmObject, VH extends RealmAdapter.ViewHolder>
extends BaseAdapter {
public static class ViewHolder {
public final View itemView;
public ViewHolder(View itemView) {
this.itemView = itemView;
this.itemView.setTag(this);
}
}
private RealmResults<E> results;
public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);
public abstract void onBindViewHolder(VH holder, int position);
public void onViewRecycled(VH holder) {
}
public void setResults(RealmResults<E> results) {
this.results = results;
notifyDataSetChanged();
}
@Override
public int getCount() {
return results == null ? 0 : results.size();
}
@Override
public E getItem(int position) {
return results == null ? null : results.get(position);
}
@Override
public long getItemId(int position) {
return -1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = onCreateViewHolder(parent, getItemViewType(position)).itemView;
} else {
onViewRecycled(getViewHolder(convertView));
}
onBindViewHolder(getViewHolder(convertView), position);
return convertView;
}
@SuppressWarnings("unchecked")
private VH getViewHolder(View view) {
return (VH) view.getTag();
}
}
@xingrz
Copy link
Author

xingrz commented Oct 21, 2014

Usage

public final class ExampleAdapter extends RealmAdapter<Example, ExampleAdapter.ViewHolder> {

    public static class ViewHolder extends RealmAdapter.ViewHolder {
        public final TextView title;

        public ViewHolder(View itemView) {
            super(itemView);
            this.title = (TextView) itemView.findViewById(R.id.title);
        }
    }

    public static interface OnItemClickListener {
        public void onItemClick(Example example);
    }

    private final LayoutInflater inflater;
    private final OnItemClickListener listener;

    public ExampleAdapter(Context context, OnItemClickListener listener) {
        this.inflater = LayoutInflater.from(context);
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.example_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Example item = getItem(position);
        holder.title.setText(item.title);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(item);
            }
        });
    }

}
ExampleAdapter adapter = new ExampleAdapter(this, new ExampleAdapter.OnItemClickListener() {
    public void onItemClick(Example example) {
        // ...
    }
});

listView.setAdapter(adapter);

adapter.setResults(realm.where(Example.class).findAll());

@rhzs
Copy link

rhzs commented Mar 21, 2015

sorry noob question, what is the main benefit using this Realm adapter?

@slidenerd
Copy link

Read this answer here from Pirate App for an even more sophisticated realm adapter that can add items remove items, swipe to delete, section the recyclerview http://stackoverflow.com/questions/28995380/best-practices-to-use-realm-with-a-recycler-view

@longnguyencse
Copy link

thank u

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