问题
I have a Room database and RecyclerView that shows a list of CardViews. One of the database columns holds Sort Index values so the CardViews can easily be sorted and moved within the RecyclerView list. I have a Dao call to update the Sort Index values whenever a CardView is moved, deleted or added.
Room requires CRUD on a background thread and the syntax I have in the AsyncTask() in the respository that calls the Dao method is wrong. I am trying to pass the individual "int" sortOrders and the "int" sortIds. What am I missing here? Or would " Executor mExecutor = Executors.newSingleThreadExecutor();" be a better solution here?
MainActivity
int sortId = -1;
int sortOrder = -1;
...
mViewModel.updateSortOrder(sortOrder, sortId);
ViewModel
...
public void updateSortOrder(int sortOrder, int sortId) {
repository.updateSortOrder(sortOrder, sortId);
}
Repository
...
public void updateSortOrder(int sortOrder, int sortId) {
new UpdateSortOrderColAsyncTask(quickcardDao).execute(sortOrder, sortId);
}
**I think this is where the syntax is not correct**
private static class UpdateSortOrderColAsyncTask extends AsyncTask<Integer, Void, Void> {
private QuickcardDao asyncTaskDao;
UpdateSortOrderColAsyncTask(QuickcardDao dao) {
asyncTaskDao = dao;
}
@Override
protected Void doInBackground(final Integer... params) {
asyncTaskDao.updateSortorder(params[0]);
return null;
}
}
Dao
@Query("UPDATE cards SET cardSortorder = :sortOrder WHERE cardId = :sortId")
void updateSortorder(int sortOrder, int sortId);
来源:https://stackoverflow.com/questions/57340870/android-update-single-room-column-with-dao-call-from-repository