【问题标题】:Android Room SQLITE Update Custom Query with MVVMAndroid Room SQLITE 使用 MVVM 更新自定义查询
【发布时间】:2020-02-08 21:54:55
【问题描述】:

我正在尝试执行一个简单的更新查询,其中将更新一行中的一列。我在网上看到的每个示例都会进行全面更新,其中更新了整行。我正在使用 MVVM - Activity、Viewmodel、Repository、AsyncTask、DAO。

在我执行异步任务之前一切正常。 doInBackground 方法需要 2 个参数,即用于标识行的主键和要更新的数据。然而,异步任务只接受对象。所以我不确定如何进行。请看下面的sn-ps:

 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);
}

【问题讨论】:

    标签: android mvvm android-asynctask android-sqlite android-room


    【解决方案1】:

    试试下面的方法怎么样?

    @Override
    protected Void doInBackground(CbtTable... cbtTables) {
        CbtTable cbtTable = cbtTables[0]
        mCbtDao.updateTwistedThinking(cdtTable.getCbtId(), cdtTable.getTwistedThinkingPK());
        return null;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 2019-08-24
      相关资源
      最近更新 更多