I am using cursor adapter for my list view. I was planning to use content resolvers to get a cursor. Now i changed my mind to give it a chance to learn Realm.
How can i get "Cursor" object by using realm? By the way, i would also be appreciate give a few snippet to sort cursor.
Thanks.
final class Contact extends RealmObject {
private CharSequence mName;
public CharSequence getName() {
return mName;
}
public void setName(CharSequence name) {
this.mName = name;
}
}
final class ContactListAdapter extends CursorRecyclerViewAdapter<ContactListAdapter.ContactHolder> implements View.OnClickListener {
private OnContactClickListener mListener;
public ContactListAdapter(Context context, Cursor cursor) {
super(context, cursor);
}
@Override
public ContactHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = getLayoutInflater().inflate(R.layout.cell_contact, parent, false);
ContactHolder contactHolder = new ContactHolder(itemView);
contactHolder.mContactPhotoImageView.setOnClickListener(this);
return contactHolder;
}
@Override
public void onBindViewHolder(ContactHolder viewHolder, Cursor cursor) {
viewHolder.mNameTextView.setText("Emre Akturk");
int position = cursor.getPosition();
viewHolder.mContactPhotoImageView.setImageResource(R.mipmap.ic_launcher);
viewHolder.mContactPhotoImageView.setTag(position);
}
@Override
public int getItemCount() {
return 5;
}
public void setOnContactClickListener(OnContactClickListener callback) {
this.mListener = callback;
}
@Override
public void onClick(View v) {
if (mListener == null) {
return;
}
int position = (int) v.getTag();
if (position == -1) {
mListener.onContactCallClicked(v, getCursor(), position);
} else {
mListener.onContactAddClicked(v, position);
}
}
public interface OnContactClickListener {
void onContactCallClicked(View v, Cursor cursor, int position);
void onContactAddClicked(View v, int position);
}
protected class ContactHolder extends RecyclerView.ViewHolder {
private TextView mNameTextView;
private CircleImageView mContactPhotoImageView;
protected ContactHolder(View itemView) {
super(itemView);
mNameTextView = (TextView) itemView.findViewById(R.id.cell_contact_name_textview);
mContactPhotoImageView = (CircleImageView) itemView.findViewById(R.id.cell_contact_photo_imageview);
}
}
}
Christian from Realm here. Realm currently doesn't expose a Cursor although it is something we want to do: https://github.com/realm/realm-java/issues/438
If you want to use a RecyclerView with Realm I would recommend this blogpost for advice on how to integrate them: http://gradlewhy.ghost.io/realm-results-with-recyclerview/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With