Android: update single Room column with Dao call from repository?

隐身守侯 提交于 2020-01-06 05:51:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!