Android Room SQLITE 使用 MVVM 更新自定义查询
Android Room SQLITE Update Custom Query with MVVM
我正在尝试执行一个简单的更新查询,其中将更新一行中的一列。我在网上看到的每个示例都会对整行进行更新。我正在使用 MVVM - Activity、Viewmodel、Repository、AsyncTask、DAO。
在我开始异步任务之前一切正常。 doInBackground 方法需要 2 个参数,用于标识行的主键和要更新的数据。然而,异步任务只接受对象。所以我不确定如何进行。请参阅以下片段:
DAO
@Query ("UPDATE CbtTable SET twistedThinkingPK = :twistedThinkingPK WHERE cbtId = :cbtId")
void updateTwistedThinking(long cbtId, long twistedThinkingPK);
ASYNC
@Override
protected Void doInBackground(CbtTable... cbtTables) {
mCbtDao.updateTwistedThinking(cbtTables[0]); // THIS THROWS THE ERROR, IT WANTS THE TWO VARIABLES FORM THE DAO UPDATE QUERY.
return null;
REPOSITORY
public void updateTwistedThinking(CbtTable cbtTable){
new UpdateTwistedThinkingAsyncTask(cbtDao).execute(cbtTable);
}
VIEW MODEL
public void updateTwistedThinking(CbtTable cbtTable){
cbtRepository.updateTwistedThinking(cbtTable);
}
ACTIVITY
public void updateTwistedThinkingCbtTable(long twistedThinkingPK){
CbtTable cbtTable = new CbtTable();
cbtTable.setTwistedThinkingPK(twistedThinkingPK);
cbtViewModel.updateTwistedThinking(cbtTable);
}
试试下面的方法怎么样?
@Override
protected Void doInBackground(CbtTable... cbtTables) {
CbtTable cbtTable = cbtTables[0]
mCbtDao.updateTwistedThinking(cdtTable.getCbtId(), cdtTable.getTwistedThinkingPK());
return null;
}
我正在尝试执行一个简单的更新查询,其中将更新一行中的一列。我在网上看到的每个示例都会对整行进行更新。我正在使用 MVVM - Activity、Viewmodel、Repository、AsyncTask、DAO。
在我开始异步任务之前一切正常。 doInBackground 方法需要 2 个参数,用于标识行的主键和要更新的数据。然而,异步任务只接受对象。所以我不确定如何进行。请参阅以下片段:
DAO
@Query ("UPDATE CbtTable SET twistedThinkingPK = :twistedThinkingPK WHERE cbtId = :cbtId")
void updateTwistedThinking(long cbtId, long twistedThinkingPK);
ASYNC
@Override
protected Void doInBackground(CbtTable... cbtTables) {
mCbtDao.updateTwistedThinking(cbtTables[0]); // THIS THROWS THE ERROR, IT WANTS THE TWO VARIABLES FORM THE DAO UPDATE QUERY.
return null;
REPOSITORY
public void updateTwistedThinking(CbtTable cbtTable){
new UpdateTwistedThinkingAsyncTask(cbtDao).execute(cbtTable);
}
VIEW MODEL
public void updateTwistedThinking(CbtTable cbtTable){
cbtRepository.updateTwistedThinking(cbtTable);
}
ACTIVITY
public void updateTwistedThinkingCbtTable(long twistedThinkingPK){
CbtTable cbtTable = new CbtTable();
cbtTable.setTwistedThinkingPK(twistedThinkingPK);
cbtViewModel.updateTwistedThinking(cbtTable);
}
试试下面的方法怎么样?
@Override
protected Void doInBackground(CbtTable... cbtTables) {
CbtTable cbtTable = cbtTables[0]
mCbtDao.updateTwistedThinking(cdtTable.getCbtId(), cdtTable.getTwistedThinkingPK());
return null;
}