Following some articles, I am trying to create an android app with the following:
- A
recyclerview
which fetches data from room database using live data. - Data structure is a list of custom objects with an attribute for ordering the data.
Features in recyclerview:
- Drag and drop for reordering data
- Swipe to delete
- UNDO action for swipe to delete
Referred articles:
- Google codelabs for room database and live data
- AndroidHive article for recyclerview swipe to delete and undo to restore deleted item
- Medium post by Paul Burke for drag/drop in recycler view
- Medium post by Paul Burke for customizing dragged item in recycler view
- SO Post to detect drop event in recycler view
My problem:
Data reordering is not updated in the room library.
Note:
I am using an attribute for data ordering
Please drop a comment if code of a particular file is required.I am not sure which code to post.
MainFragment.java (Code to reorder data, which is not working)
// To handle recycler view item dragging
@Override
public void onItemMove(int fromPosition, int toPosition) {
// Log
Log.e(TAG, "Item moved from " + fromPosition + " to " + toPosition);
// Move the item within the recycler view
mainRecyclerViewAdapter.moveItem(fromPosition, toPosition);
}
// To handle recycler view item drop
@Override
public void onItemDragged(int fromPosition, int toPosition) {
// Log
Log.e(TAG, "Item dragged from " + fromPosition + " to " + toPosition);
mainActivityViewModel.moveWord(fromPosition, toPosition);
}
MainActivityViewModel.java
public void moveWord(int fromPosition, int toPosition) {
// Move word
wordRepository.move(fromPosition, toPosition);
}
WordRepository.java
public void move(int from, int to) {
new moveAsyncTask(wordDao).execute(from, to);
}
// Async update task
private static class moveAsyncTask extends AsyncTask<Integer, Void, Void> {
// Dao
private WordDao asyncTaskDao;
// Constructor
moveAsyncTask(WordDao wordDao) {
// Get dao
asyncTaskDao = wordDao;
}
@Override
protected Void doInBackground(final Integer... params) {
int from = params[0];
int to = params[1];
if (from > to) {
// Move upwards
asyncTaskDao.getAllWordsBetween(to, from - 1).forEach(wordToUpdate -> {
// Update word number
wordToUpdate.decreaseSNo();
// Update word in database
update(wordToUpdate);
});
asyncTaskDao.getWordWithNo(from).forEach(wordToUpdate -> {
// Update word number
wordToUpdate.setSno(to);
// Update word in database
update(wordToUpdate);
});
} else {
// Move downwards
asyncTaskDao.getAllWordsBetween(from + 1, to).forEach(wordToUpdate -> {
// Update word number
wordToUpdate.increaseSNo();
// Update word in database
update(wordToUpdate);
});
asyncTaskDao.getWordWithNo(from).forEach(wordToUpdate -> {
// Update word number
wordToUpdate.setSno(to);
// Update word in database
update(wordToUpdate);
});
}
return null;
}
}
WordDao.java
@Query("SELECT * FROM words WHERE sno >= :low AND sno <= :high")
List<Word> getAllWordsBetween(int low, int high);
@Query("SELECT * FROM words WHERE sno == :sNo")
List<Word> getWordWithNo(int sNo);