Reputation: 327
I am using the RealmRecyclerView from this post:https://realm.io/news/android-realm-listview/ In this ToDo app, when item is swiped, it is automatically deleted from RecyclerView as well as Realm Database. I want to get notified when an item is deleted, also which item is deleted so I can perform some action with that item. I tried using the Realm Change Listener, but that is invoked every time when a Realm Transaction is committed. So it is invoked even when a new item is added. How do I do this? Is it possible with normal RecyclerView?
Upvotes: 2
Views: 945
Reputation: 1
While Realm may not provide a callback for when an item is deleted, there is a way to know when items are deleted when using the RealmRecyclerView
from this post.
The adapter (RealmBasedRecyclerViewAdapter
) will call onItemSwipedDismiss(int position)
whenever an item is swiped for dismissal. In your subclass of this adapter, you can override this method to add some extra logic.
For example, in my Recycler View, I want to give users the option to Undo a deletion. So, I override onItemSwipedDismiss(int position)
and access the fields of the object being deleted. (In my case, this object is fairly small -- only three fields -- so this isn't too unwieldy). Then I call the super method: super.onItemSwipedDismiss(position);
which will animate the deletion and remove it from Realm.
Then, I create a Snackbar with an action that re-creates the Realm object from the saved fields. Once it's created, it immediately goes back into the recycler view.
Here's a skeleton of the implementation of this method override:
@Override
public void onItemSwipedDismiss(int position) {
// Gather the object's fields, if you want:
YourObject objectToDelete = realmResults.get(position);
final String title = objectToDelete.getTitle();
final long timestamp = objectToDelete.timestamp;
// Perform delete and animation:
super.onItemSwipedDismiss(position);
// Add code here depending on what you want to do
// (for example, you could add a Snackbar that undoes
// the deletion by "resurrecting" your deleted object)
}
Upvotes: 0
Reputation: 327
According to the answer, they have not yet added this feature. But if you want to achieve this, you can use normal ReacyclerView using the existing RealmAdapter and everything as it is.
Here's how to do it:-
Remove the RealmRecyclerView and add the normal RecyclerView:-
1. Add the normal RecyclerView from the support library.
2. Initialize the recyclerview with the existing adapter i.e the adapter class that extends RealmBasedRecyclerViewAdapter, no need to make a new adapter
recyclerView=(RecyclerView)findViewById(R.id.realm_recyeler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new FilterAdapter(this,results,true,true);
recyclerView.setAdapter(adapter);
3. Next, we will use the ItemTouchHelper class to implement the swipe to dismiss for the RecyclerView :-
ItemTouchHelper.SimpleCallback callback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
adapter.remove(viewHolder.getAdapterPosition(),alarmintent);
}
};
I've made a method in my adapter to remove item(shown below), you can do it here as well
The viewHolder.getAdapterPositon() gives the position of item swiped, it is passed to delete the RealmObject from Realm DB at given position(shown below)
0 -> drag flag - since I am not implement drag to move items, I've kept it as zero
ItemTouchHelper.RIGHT - swipe flags - These say in which direction the swipe to dismiss is set
ItemTouchHelper.RIGHT - swipe in right direction to dismiss
ItemTouchHelper.LEFT - swipe in left direction to dismiss
To support both directions, pass- ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
itemTouchHelper.attachToRecyclerView(recyclerView);
Create a new ItemTouchHelper object with above callback
Attach the ItemTouchHelper to RecyclerView
4. Here's how to remove the item (below is the code of my remove method of adapter):-
public void remove(int position)
{
RealmConfiguration configuration = new RealmConfiguration.Builder(context).deleteRealmIfMigrationNeeded().build();
realm = Realm.getInstance(configuration);
realm.beginTransaction();
realmResults.deleteFromRealm(position);
realm.commitTransaction();
notifyItemRemoved(position);
}
deleteFromRealm method is used to delete item at given position
call the notifyItemRemoved(position) to indicate item is removed at specified position
5. That's it, very easy and no need to create new adapters etc.
Upvotes: 1
Reputation: 1285
At this moment (with version V1.1.0), Realm does not provide a callback when a RealmObject is deleted. This is true for all types of data/views (listView, RecyclerView, RealmObject, RealmResults etc), also true if you query data asObservable or use a change listener.
However it does make sense to send an empty object, when the queried object is deleted from Realm, but since its a breaking change in Realm, we will have to wait for V2.0.
More details - https://github.com/realm/realm-java/issues/3138
Upvotes: 1